retainstrong和copy讨论,有建议的的童鞋请留言
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:34
总结下我的懂得:
strong weak是ARC惹人的,strong相当于retain,然则对于有的类型,例如NSString,则应用strong相当于应用copy
如许直接用strong就可以直接处理retain和strong的情况了。
weak相当于assign,然则比后者多一点:对象被干掉落时会将weak引用设为nil,而对nil发送消息都不会导致崩溃
NSString *strongStr;
不然weak引用为野指针,会出现问题
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString *copStr;
}
@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(@"strong string:%@", self.strongStr);
NSLog(@"retain sting:%@", self.retainStr);
NSLog(@"copy string:%@", self.copStr);
输出结不雅:
2014-05-02 13:31:17.660 TestMultipleThreadDownload[552:60b] retain sting:abcd123
这里说strong 有copy的功能是缺点的哦。实验例子
2014-05-02 13:31:17.662 TestMultipleThreadDownload[552:60b] strong string:abcd123
NSString *retainStr;
2014-05-02 13:31:17.663 TestMultipleThreadDownload[552:60b] copy string:abcd
strong weak是ARC惹人的,strong相当于retain,然则对于有的类型,例如NSString,则应用strong相当于应用copy
如许直接用strong就可以直接处理retain和strong的情况了。
weak相当于assign,然则比后者多一点:对象被干掉落时会将weak引用设为nil,而对nil发送消息都不会导致崩溃
NSString *strongStr;
不然weak引用为野指针,会出现问题
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString *copStr;
}
@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(@"strong string:%@", self.strongStr);
NSLog(@"retain sting:%@", self.retainStr);
NSLog(@"copy string:%@", self.copStr);
输出结不雅:
2014-05-02 13:31:17.660 TestMultipleThreadDownload[552:60b] retain sting:abcd123
这里说strong 有copy的功能是缺点的哦。实验例子
2014-05-02 13:31:17.662 TestMultipleThreadDownload[552:60b] strong string:abcd123
NSString *retainStr;
2014-05-02 13:31:17.663 TestMultipleThreadDownload[552:60b] copy string:abcd