2024-10-22 14:15:21
一、属性修饰符
读写属性: (readwrite/readonly)
语义特性:(内存有关):(assign/retain/copy)
原子特性:(和线程安全有关): (atomicit/nonatomic)
readonly:只读,属性只会生成getter方法,不会生成setter.
readwrite:既可读又可写.属性自动生成setter和getter方法.(默认的读写特性)
getter= 方法名:指定生成getter方法时的方法名.
setter= 方法名:指定生成setter方法时的方法名.
assign: 默认类型,setter方法直接赋值,而不进行retain操作,也可以只针对对象,只是做简单的赋值操作. 默认的语义特性.
retain: 针对于对象类型,setter方法对参数进行release旧值,再retain新值。
copy: setter方法进行Copy操作,与retain一样, 针对于对象类型,会拷贝一个新的对象,将新的对象的引用计数加1
nonatomic: 禁止多线程,变量保护,提高性能
atomic:原子特性,保证线程安全.系统默认的原子特性.
weak:指针指向的地址一旦被释放,这些指针都将被赋值为nil。这样的好处能有效的防止野指针。消除循环引用 。
二、 @dynamic 关键字
@dynamic dynamicStr; // 告诉编译器,不自定生成getter和setter方法,避免编译期间产生警告,然后由自己实现存取方法。
三、 @synthesize 关键字
@synthesize strongStr = _strongStr; // 可以定义 与变量名不相同的getter和setter的命名,籍此来保护变量不会被不恰当的访问, 编译器期间,让编译器自动生成getter/setter方法 当有自定义的存或取方法时,自定义会屏蔽自动生成该方法
四、重定义setter方法getter方法
// 重定义setter方法
// 重定义getter方法
五、不可变字符串 NSString
// 运行log日志:
2021-03-11 17:30:08.112340+0800 TestObject-C[93449:1623723] temp1: 不可变字符串, 0x10df9e778
2021-03-11 17:30:08.112529+0800 TestObject-C[93449:1623723] str: 不可变字符串, 0x10df9e778
2021-03-11 17:30:08.112610+0800 TestObject-C****[****93449:1623723****]**** strongStr: ****不可变字符串****, 0x10df9e778
六、可变字符串 NSMutableString
// 一、将不可变字符串赋值给可变字符串对象。
// 赋值可变字符串对象后,对象仍然是不可变对象
2021-03-11 17:30:08.114159+0800 TestObject-C[93449:1623723] mTemp: 不 可变字符串1, 0x10df9e878
2021-03-11 17:30:08.114393+0800 TestObject-C[93449:1623723] mCopyStr: 不 可变字符串1, 0x10df9e878
2021-03-11 17:30:08.114697+0800 TestObject-C****[****93449:1623723****]**** mStrongStr: 不 可变字符串****1, 0x10df9e878
// 一、将可变字符串赋值给可变字符串对象。
2021-03-11 19:38:34.493763+0800 TestObject-C[95274:1718783] mTemp2: 可变字符串2, 0x600002710ea0, __NSCFString
2021-03-11 19:38:34.493923+0800 TestObject-C[95274:1718783] mCopyStr: 可变字符串2, 0x6000027110e0, __NSCFString
2021-03-11 19:38:34.494037+0800 TestObject-C****[****95274:1718783****]**** mStrongStr: ****可变字符串****2, 0x600002710ea0, __NSCFString
2021-03-11 19:38:34.494422+0800 TestObject-C[95274:1718783] mTemp2: 可变字符串211, 0x600002710ea0, __NSCFString
2021-03-11 19:38:34.494697+0800 TestObject-C[95274:1718783] mCopyStr: 可变字符串2, 0x6000027110e0, __NSCFString
2021-03-11 19:38:34.494903+0800 TestObject-C****[****95274:1718783****]**** mStrongStr: ****可变字符串****211, 0x600002710ea0, __NSCFString
七、富文本字符串 NSMutableAttributedString
2021-03-11 19:34:52.041131+0800 TestObject-C[95207:1715249] attriStr: 我是富文本我是富文本我是富文本我是富文本, 0x600000a5bc00, NSConcreteMutableAttributedString
2021-03-11 19:34:52.041307+0800 TestObject-C[95207:1715249] mutableAttri: 我是富文本我是富文本我是富文本我是富文本, 0x600000a5be40, NSConcreteAttributedString
2021-03-11 19:34:52.041425+0800 TestObject-C****[****95207:1715249****]**** mutableStrongAttri: ****我是富文本我是富文本我是富文本我是富文本****, 0x600000a5bc00, NSConcreteMutableAttributedString
2021-03-11 19:34:52.041943+0800 TestObject-C[95207:1715249] attriStr: 我是富文本我是富文本我是富文本我是富文本---我是新添加的, 0x600000a5bc00, NSConcreteMutableAttributedString
2021-03-11 19:34:52.042281+0800 TestObject-C****[****95207:1715249****]**** mutableStrongAttri: ****我是富文本我是富文本我是富文本我是富文本****---****我是新添加的****, 0x600000a5bc00, NSConcreteMutableAttributedString
八、不可变数组NSArray
//一、 不可变数组赋值NSArray的对象
2021-03-11 19:04:42.636207+0800 TestObject-C[94739:1689350] arr: (1, 2,
3), 0x600001d5c8d0
2021-03-11 19:04:42.636411+0800 TestObject-C[94739:1689350] persons: ( 1, 2, 3), 0x600001d5c8d0
2021-03-11 19:04:42.636538+0800 TestObject-C[94739:1689350] students: (1, 2, 3), 0x600001d5c8d0
2021-03-11 19:04:42.636646+0800 TestObject-C[94739:1689350] arr: (1, 2, 3), 0x600001d5c8d0
2021-03-11 19:04:42.636767+0800 TestObject-C[94739:1689350] students: (1, 2,3), 0x600001d5c8d0, __NSArrayI
// 二、不可变数组赋值给NSMutableArray的对象
**2021-03-11 19:15:59.046728+0800 TestObject-C[94934:1699039] arr: (1,2,3), 0x600002f6c000
2021-03-11 19:15:59.046878+0800 TestObject-C[94934:1699039] cars: (1, 2,3), 0x600002f6c000, __NSArrayI2021-03-11 19:15:59.046982+0800 TestObject-C[94934:1699039] dogs: (1,2,3), 0x600002f6c000, __NSArrayI
九、可变数组 NSMutableArray
2021-03-11 19:22:39.183745+0800 TestObject-C[95044:1705082] arr2: (2,4,10,12), 0x600000fefea0
2021-03-11 19:22:39.184067+0800 TestObject-C[95044:1705082] cars: (2,4,10), 0x600000fef240, __NSArrayI
**2021-03-11 19:22:39.184272+0800 TestObject-C[95044:1705082] dogs: (2,4,10,12), 0x600000fefea0, __NSArrayM
十、不可变字典 NSDictionary
// 不可变数组赋值给NSDictionary对象
2021-03-11 19:25:32.789615+0800 TestObject-C[95102:1708378] dic: {key1 = 1;key2 = value;}, 0x6000028799c0**
2021-03-11 19:25:32.789801+0800 TestObject-C[95102:1708378] personDic: { key1 = 1;key2 = value;}, 0x6000028799c0, __NSDictionaryI
2021-03-11 19:25:32.789919+0800 TestObject-C[95102:1708378] studentDic: { key1 = 1;key2 = value;}, 0x6000028799c0, __NSDictionaryI
十一、可变字典 NSMutableDictionary
2021-03-11 19:59:28.739545+0800 TestObject-C[96373:1735182] mDic: { key1 = 10;key2 = value2;}, 0x6000017f0700, __NSDictionaryM
**2021-03-11 19:59:28.740177+0800 TestObject-C[96373:1735182] carDic: {key1 = 10;key2 = value2;}, 0x6000017f07a0, __NSFrozenDictionaryM
2021-03-11 19:59:28.740359+0800 TestObject-C[96373:1735182] dogDic: { key1 = 10;key2 = value2;}, 0x6000017f0700, __NSDictionaryM
2021-03-11 20:05:24.028840+0800 TestObject-C[96469:1740141] mDic: {key1 = 10; key2 = value2;key4 = value4;}, 0x6000011bd7e0, __NSDictionaryM
2021-03-11 20:05:24.028930+0800 TestObject-C[96469:1740141] carDic: { key1 = 10; key2 = value2;}, 0x6000011bd820, __NSFrozenDictionaryM
2021-03-11 20:05:24.029028+0800 TestObject-C[96469:1740141] dogDic: {key1 = 10;key2 = value2;key4 = value4;}, 0x6000011bd7e0, __NSDictionaryM
十二、不可变集合 NSSet
// 一、不可变NSSet赋值给 NSSet对象
2021-03-11 20:09:49.418694+0800 TestObject-C[96545:1744249] set: {( 1,2,3)}, 0x600003fd2e80, __NSSetI
2021-03-11 20:09:49.418826+0800 TestObject-C[96545:1744249] personSet: {(1,2,3)}, 0x600003fd2e80, __NSSetI
2021-03-11 20:09:49.418914+0800 TestObject-C[96545:1744249] studentSet: {(1,2,3)}, 0x600003fd2e80, __NSSetI
// 二、不可变NSSet赋值给 NSMutableSet对象
2021-03-11 20:09:49.419281+0800 TestObject-C[96545:1744249] carSet: {(1,2,3)}, 0x600003fd2e80, __NSSetI
2021-03-11 20:09:49.419362+0800 TestObject-C[96545:1744249] dogSet: {(1, 2,3)}, 0x600003fd2e80, __NSSetI
十三、可变集合 NSMutableSet
2021-03-11 20:18:06.896997+0800 TestObject-C[96705:1752827] mSet: {(3,1,2,5)}, 0x600001e0a700, __NSSetM
2021-03-11 20:18:06.897218+0800 TestObject-C[96705:1752827] carSet: {( 1,2,3)}, 0x600001014bd0, __NSSetI
2021-03-11 20:18:06.897340+0800 TestObject-C[96705:1752827] dogSet: {(3,1,2, 5)}, 0x600001e0a700, __NSSetM