总结下我的理解:
strong weak是ARC引入的,strong相当于retain,但是对于有的类型,例如NSString,则使用strong相当于使用copy
这样直接用strong就可以直接处理retain和strong的情况了。
weak相当于assign,但是比后者多一点:对象被干掉时会将weak引用设为nil,而对nil发送消息都不会导致崩溃
否则weak引用为野指针,会出现问题
这里说strong 有copy的功能是错误的哦。实验例子
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString *retainStr;
NSString *copStr;
NSString *strongStr;
}
@property (retain, nonatomic) NSString *retainStr;
@property (strong, nonatomic) NSString *strongStr;
@property (copy, nonatomic) NSString *copStr;
@end
NSMutableString *newStr = [NSMutableString stringWithFormat:@"abcd"];
self.retainStr = newStr;
self.strongStr = newStr;
self.copStr = newStr;
[newStr appendString:@"123"];
NSLog(@"retain sting:%@", self.retainStr);
NSLog(@"strong string:%@", self.strongStr);
NSLog(@"copy string:%@", self.copStr);
输出结果:
2014-05-02 13:31:17.660 TestMultipleThreadDownload[552:60b] retain sting:abcd123
2014-05-02 13:31:17.662 TestMultipleThreadDownload[552:60b] strong string:abcd123
2014-05-02 13:31:17.663 TestMultipleThreadDownload[552:60b] copy string:abcd