- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想知道在决定 ObjectiveC 中的给定属性应该是 retain
还是 copy
时,您是否遵循了经验法则?
你如何决定它应该是哪个?
最佳答案
通常你使用 copy
来保证安全,这些类有可变的变体,比如 NSString
,NSArray
,其他集合类,等等。看看为什么,考虑这里发生的事情......
从前,
@interface MyClass : NSObject
@property (retain) NSString *happyString;
- (void)rejoice;
@end
然后有一天,
- (void)bigBadMethod {
MyClass *myObject = [[[MyClass alloc] init] autorelease];
NSMutableString *theString = [NSMutableString stringWithString:@"I'm happy!"];
myObject.happyString = theString; // this is allowed because NSMutableString inherits from NSString
[myObject rejoice]; // prints "I'm happy!"
当突然...
[theString setString:@"BRAAAAIIINNNSSSSS"];
[myObject rejoice]; // prints "BRAAAAIIINNNSSSSS"
}
你不会想要那样吧?所以如果你不想在你不看的时候发生变异,请使用 @property (copy)
!
关于objective-c - 使用@property(copy) 与@property(retain) 的经验法则是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7115337/
我正在为一个类(class)项目制作一个 C# 应用程序。我想确保一个字符串具有三个值之一。通常,在网络应用程序中,我会在客户端使用 javascript 进行验证。但是,这目前是一个控制台应用程序。
我注意到了 int i=10000000; boolean isPrime= false; while(!isPrime){ i++; Syst
一般来说,类构造函数应该接受的最大参数数量是多少?我正在开发一个需要大量初始化数据(目前有 10 个参数)的类。但是,带有 10 个参数的构造函数感觉不对。这让我相信我应该为每条数据创建一个 gett
改善网页加载时间的一种方法是内联其 css - 因为 css 会阻塞渲染。但是,内联 css 会阻止浏览器缓存它。因此,一般建议仅内联“小”css。 但是什么才算“小”呢?我在网上搜索了一些经验法则,
我正在尝试了解如何正确使用 Java 的执行器。我意识到将任务提交给 ExecutorService 有其自身的开销。但是,我很惊讶地看到它竟然这么高。 我的程序需要以尽可能低的延迟处理大量数据(股票
我读过几篇 GWT 开发人员博客,其中提到了一些“经验法则”,即如果生成的 JavaScript 代码达到 100 行或更多,最好自己编写 JavaScript(而不是通过 GWT 生成它) . 我的
最近我偶然发现了一个非常大的性能改进——我说的是 4 倍的改进——只需更改一行代码。我刚刚将 std::sort 调用更改为 concurrency_parallel 排序 // Get a cont
我在 javascript 中使用 setTimeout() 函数来允许在解析一些 xml 数据时显示一个显示“正在加载”的弹出窗口。我发现,在足够小的延迟值(低于 10 毫秒)下,在浏览器卡住一段时
Leonid 在他的书的第四章中写道:“...Module、Block 和 With。这些结构在 Mathematica Book 和 Mathematica Help 中有详细解释,所以我在这里简单
我是一名优秀的程序员,十分优秀!