一.先介绍 copy.strong.weak 的区别,如代码所示 @property(copy,nonatomic)NSMutableString*aCopyMStr; @property(strong,nonatomic)NSMutableString*strongMStr; @property(weak,nonatomic)NSMutableString*weakMStr; @property(assign,nonatomic)NSMutableString*assignMStr; NSMu…
- (void)fun{  NSString* str = [[NSString alloc] initWithString:@"string"];  NSLog(@"%d", [str retainCount]);}可以看到 str的计数器为-1 而把initWithString换成initWithFormat 计数器就是1 ------解决方案--------------------------------------------------------没有区别…
copy 和 strong(retain) 区别 1. http://blog.csdn.net/itianyi/article/details/9018567 大部分的时候NSString的属性都是copy,那copy与strong的情况下到底有什么区别呢? 比如: @property (retain,nonatomic) NSString *rStr; @property (copy, nonatomic)   NSString *cStr; - (void)test: { NSMutabl…
atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作 1)atomic 设置成员变量的@property属性时,atomic是默认值,提供多线程安全 在多线程环境下,原子操作是必要的,否则有可能引起错误的结果.加了atomic后setter函数会变成下面这样: {lock} if (property != newValue) { [property release]; property = [newValue retain]; } {unlock} 2)n…
1.NSString类型的属性为什么用copy NSString类型的属性可以用strong修饰,但会造成一些问题,请看下面代码 #import "ViewController.h" @interface ViewController () @property(nonatomic,strong) NSString *nameStrong; @property(nonatomic,copy) NSString *nameCopy; @end @implementation ViewCon…
以下内容来自Stackflow的详解 1.Nonatomicnonatomic is used for multi threading purposes. If we have set the nonatomic attribute at the time of declaration, then any other thread wanting access to that object can access it and give results in respect to multi-th…
@property(copy,nonatomic)NSMutableString*copyStr; @property(strong,nonatomic)NSMutableString*strongStr; @property(weak,nonatomic)NSMutableString*weakStr; @property(assign,nonatomic)NSMutableString*assignStr; NSMutableString* originStr = [[NSMutableSt…
举个例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作:  1 在堆上分配一段内存用来存储@' MM的三室两厅 ' ,比如:内存地址为 0X1111  内容为 ' MM的三室两厅' ,  2 在栈上分配一段内存用来存储 houseForWife ,比如:地址为 0XAAAA  内容自然为 0X1111 下面分别看下(assign,retain,copy):  1.assign…
例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作:  1 在堆上分配一段内存用来存储@' MM的三室两厅 ' ,比如:内存地址为 0X1111  内容为 ' MM的三室两厅' ,  2 在栈上分配一段内存用来存储 houseForWife ,比如:地址为 0XAAAA  内容自然为 0X1111 下面分别看下(assign,retain,copy):  1.assign的情…
总结 关于属性的这些选项的学习,做一下总结: 所有的属性,都尽可能使用nonatomic,以提高效率,除非真的有必要考虑线程安全. NSString:通常都使用copy,以得到新的内存分配,而不只是原来的引用. strong:对于继承于NSObject类型的对象,若要声明为强使用,使用strong,若要使用弱引用,使用__weak来引用,用于解决循环强引用的问题. weak:对于xib上的控件引用,可以使用weak,也可以使用strong. __weak:对于变量的声明,如果要使用弱引用,可以使…