简体   繁体   English

ARC和非ARC NSString初始化

[英]ARC and Non-ARC NSString Initialization

Lately, I have been using Automatic Reference Counting in my Objective-C/iOS programs, and have really been enjoying the feature. 最近,我一直在Objective-C / iOS程序中使用自动引用计数,并且真的很喜欢这个功能。

One thing I don't understand about it is the proper method to initialize an NSString . 我不明白的一件事是初始化NSString的正确方法。 I have seen this method used with both ARC and non-ARC projects: 我已经看到这种方法与ARC和非ARC项目一起使用:

NSString *myClassicString = [[NSString alloc] initWithFormat:@"My great non-ARC string!"];

I have also found that the following method can be used for initializing an NSString in ARC, and I prefer it because of the convenience: 我还发现以下方法可用于初始化ARC中的NSString ,我更喜欢它,因为它的方便性:

NSString *myARCString = [NSString stringWithFormat:@"My new simple initialization string!"];

Is there any difference between these two? 这两者有什么区别吗? Is there a proper way? 有没有正确的方法? Is either one better? 两个更好吗?

Pre-ARC, there was a significant difference between the two methods. 在ARC之前,两种方法之间存在显着差异。 The first, the alloc init method, produced an object that was owned by the caller- it had to be manually released or else there would be a memory leak. 第一个是alloc init方法,它产生了一个由调用者拥有的对象 - 它必须手动释放,否则就会出现内存泄漏。 The other, +stringWithFormat: is a convenience method that produces an autoreleased object that did not need to be explicitly released. 另一个是+stringWithFormat:是一种方便的方法,可以生成一个不需要显式释放的自动释放对象。

With ARC, this difference is still significant. 使用ARC,这种差异仍然很大。 Autoreleased objects still pile up because autorelease pools are only drained every cycle of the run loop, even with ARC, so if you have a loop which creates lots of objects that are autoreleased (ie they use NSString convenience methods or others), your peak memory footprint might rise. 自动释放的对象仍然堆积,因为自动释放池只在运行循环的每个循环中耗尽,即使使用ARC,所以如果你有一个循环创建了大量自动释放的对象(即它们使用NSString方便方法或其他),你的峰值内存足迹可能上升。 Using the -alloc -init version is better for this reason. 出于这个原因,使用-alloc -init版本更好。 Past that, there is very little difference with ARC. 过去,与ARC的差别很小。

This answer gives a nice explanation of what is happening here with autorelease pools, and the concepts that Jon talks about still apply to ARC code, because ARC doesn't get rid of -retain , -release , and -autorelease , it just inserts those pieces of memory management code for you. 这个答案很好地解释了自动释放池在这里发生了什么,Jon谈到的概念仍然适用于ARC代码,因为ARC没有摆脱-retain-release-autorelease ,它只是插入那些你的内存管理代码。

All that being said, you probably don't need to worry about the difference between the two methods of creating a string. 总而言之,您可能不需要担心创建字符串的两种方法之间的区别。 Whichever feels better to you and makes more concise and easily readable code will be the better method. 无论哪种感觉更好,并使更简洁,易读的代码将是更好的方法。

Either is just fine. 要么就好了。 They are treated differently if you are manually managing memory, but none of that matters if you are using ARC. 如果您手动管理内存,则会对它们进行不同的处理,但如果您使用ARC,则无关紧要。

Both methods are the same. 两种方法都是一样的。 None is better than the other in term of NSString initialization. NSString初始化方面,没有比另一个好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM