objective-c - "stringWithString"与 "alloc ... initWithString ... autorelease"

标签 objective-c cocoa string memory

我看到它声称以下内容“几乎相同”:

foo([NSString stringWithString:@"blah"])                       # version 1
foo([[[NSString alloc] initWithString:@"blah"] autorelease])   # version 2

以上实际上是等同的还是有任何细微差别? 偏爱其中一个的原因是什么?

最佳答案

它们是等价的,但我更喜欢“stringWithString”,因为它更简洁。

关于objective-c - "stringWithString"与 "alloc ... initWithString ... autorelease",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/432322/

相关文章:

objective-c - UITableViewCell 问题 indexPath.row 显示不正确

ios - NSStream 不提供任何数据

objective-c - 无法使用不同的 NSURL 凭据进行身份验证(使用旧的已删除凭据)

objective-c - NSNotification VS KVO

string - 声明char以防 rust

iphone - NSOperation 取消 : NSInvocationOperation or NSOperation subclass?

ios - 向服务器发送 POST 请求时应用程序卡住

xcode - 一次构建一个元素的数组

android - Java代码中的硬编码字符串与@string - Android

java 字符串和哈希集成员资格匹配