Cocoa 哪种方式创建对象?例如NSArray

《programming is iOS4》有一段写到:

Many classes provide the programmer with two equivalent ways to obtain an object: either an autoreleased object or an object that you create yourself with alloc and some form of init. So, for example, NSArray supplies both the class method arrayWith- Objects: and the instance method initWithObjects:. Which should you use? On the whole, Apple would prefer you to lean toward initWithObjects:. In general, where you can generate an object with alloc and some form of init, they’d like you to do so. That way, you are in charge of releasing the object. This policy will prevent your objects from hanging around in the autorelease pool and will keep your use of memory as low as possible.


大致内容是,一般Cocoa对象都提供两种方式创建,如NSArray提供 arrayWith- Objects 和 initWithObjects两种。总体来说,苹果建议使用第二种,因为可以手动管理对象的内存释放。第一种类方法创建,其实是把对象放入autorelease pool ,在适当的时候释放,这样的话容易造成内存升高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值