V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
kongK
V2EX  ›  iDev

NSMutableArray

  •  
  •   kongK · 2015-06-05 10:08:59 +08:00 · 3235 次点击
    这是一个创建于 3220 天前的主题,其中的信息可能已经有所发展或是发生改变。

    NSMutableArray *entities = @[].mutableCopy;

    NSMutableArray *entities = [NSMutableArray array];
    有什么本质上的区别和应用场景,求大神解答

    7 条回复    2015-06-08 09:27:01 +08:00
    xfxly
        1
    xfxly  
       2015-06-05 10:44:19 +08:00
    [NSMutableArray array] 自动释放
    ibremn
        2
    ibremn  
       2015-06-05 10:51:00 +08:00   ❤️ 1
    前者相当于从一个全局单例执行mutableCopy,retainCount是1,在当前作用域就可以被释放。

    后者就是 [[[NSMutableArray alloc] init] autorelease],retainCount是2,被加入了AutoreleasePool,至少需要AutoreleasePool清空后才能被释放。

    实际用起来没区别,后者性能稍微高一点点,写法不同而已。
    我偏爱前者。
    pythons
        3
    pythons  
       2015-06-05 13:46:29 +08:00
    这种问题蛋疼。
    kobe1941
        4
    kobe1941  
       2015-06-05 18:08:03 +08:00
    @ibremn 前者的写法有可能坑队友,我喜欢跟官方保持一致。
    vixvix
        5
    vixvix  
       2015-06-06 02:38:34 +08:00
    @ibremn [[NSMutableArray alloc] init] 的 retain count也是1
    Vienta
        6
    Vienta  
       2015-06-06 09:33:22 +08:00
    前一种写法装逼吧,个人喜好按照官方的来
    kongK
        7
    kongK  
    OP
       2015-06-08 09:27:01 +08:00
    @ibremn 之前也看了一个外国友人写的代码是这么写的,没搞懂为什么, 感谢你的解答
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3572 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 836ms · UTC 04:59 · PVG 12:59 · LAX 21:59 · JFK 00:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.