一、属性修饰符
@property (nonatomic, copy) NSString *str;
@property (nonatomic, strong) NSString *str;
@property (nonatomic, copy, readwrite) NSString *str;
@property (nonatomic, copy, readonly) NSString *str;
@property (nonatomic, assign) BOOL isSuccess;
@property (nonatomic, retain) NSString *str;
@property (atomic, copy) NSString *str;
读写属性: (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。这样的好处能有效的防止野指针。消除循环引用 。
@property (noatomic, weak) XXXViewDelegate *delegate;
二、@dynamic 关键字
@property (nonatomic, copy) NSString *dynamicStr;
{
NSString *_dynamicStrValue; // 实例变量
}
@dynamic dynamicStr; // 告诉编译器,不自定生成getter和setter方法,避免编译期间产生警告,然后由自己实现存取方法。
-(NSString *)dynamicStr {
return _dynamicStrValue;
}
-(void)setDynamicStr:(NSString *)dynamicStr {
_dynamicStrValue = dynamicStr;
}
三、@synthesize 关键字
@property (nonatomic, copy) NSString *strongStr;
@synthesize strongStr = _strongStr; // 可以定义 与变量名不相同的getter和setter的命名,籍此来保护变量不会被不恰当的访问, 编译器期间,让编译器自动生成getter/setter方法 当有自定义的存或取方法时,自定义会屏蔽自动生成该方法
- (NSString *)strongStr {
return _strongStr;
}
- (void)setStrongStr:(NSString *)strongStr {
_strongStr = strongStr;
}
四、重定义setter方法getter方法
// 重定义setter方法
@property (nonatomic, copy, setter=setStrAction:) NSString* setStr;
// 重定义getter方法
@property (nonatomic, copy, getter=getStrAction) NSString *getStr;
- (NSString *)getStrAction{
return _getStr;
}
- (void)setStrAction:(NSString *)setStr {
_setStr = setStr;
}
五、不可变字符串NSString
@property (nonatomic, copy) NSString *str;
@property(nonatomic, strong) NSString *strongStr;
NSString *temp = @"不可变字符串";
self.str = temp; // coy 修饰的str
self.strongStr = temp; // strong 修饰的strongStr
NSLog(@"temp1: %@, %p", temp, temp);
NSLog(@"temp2: %@, %p", temp, temp);
NSLog(@"str: %@, %p", self.str, self.str);
NSLog(@"strongStr: %@, %p", self.strongStr, self.strongStr);
// 运行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
@property (nonatomic, copy) NSMutableString* mCopyStr;
@property(nonatomic, strong) NSMutableString *mStrongStr;
// 一、将不可变字符串赋值给可变字符串对象。
// 赋值可变字符串对象后,对象仍然是不可变对象
NSString *mTemp = @"不可变字符串1";
self.mCopyStr = mTemp;
self.mStrongStr = mTemp;
NSLog(@"mTemp: %@, %p", mTemp, mTemp)
NSLog(@"mCopyStr: %@, %p", self.mCopyStr, self.mCopyStr);
NSLog(@"mStrongStr: %@, %p", self.mStrongStr, self.mStrongStr);
// 会crash: 因为_mCopyStr 运行时是不可变字符串 仍然是不可变字符串,所以没有appendString方法实现,所以执行会crash。
[_mCopyStr appendString:@"pingjie"]; // 会crash
// 会crash: 因为_mStrongStr 运行时是不可变字符串 仍然是不可变字符串,所以没有appendString方法实现,所以执行会crash。
[_mStrongStr appendString:@"pingjie"]; // 会crash
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
// 一、将可变字符串赋值给可变字符串对象。
NSMutableString *mTemp2 = [NSMutableString stringWithFormat:@"%@", @"可变字符串2"];
self.mCopyStr = mTemp2;
self.mStrongStr = mTemp2;
NSLog(@"mTemp2: %@, %p, %@", mTemp2, mTemp2, [mTemp2 class]);
NSLog(@"mCopyStr: %@, %p, %@", self.mCopyStr, self.mCopyStr, [_mCopyStr class]);
NSLog(@"mStrongStr: %@, %p, %@", self.mStrongStr, self.mStrongStr, [_mStrongStr class]);
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
// [_mCopyStr appendString:@"10"]; // 会crash 由于_mCopyStr是copy修饰 运行时是不可变对象。
[_mStrongStr appendString:@"11"];
NSLog(@"mTemp2: %@, %p, %@", mTemp2, mTemp2, [mTemp2 class]);
NSLog(@"mCopyStr: %@, %p, %@", self.mCopyStr, self.mCopyStr, [_mCopyStr class]);
NSLog(@"mStrongStr: %@, %p, %@", self.mStrongStr, self.mStrongStr, [_mStrongStr class]);
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
@property (nonatomic, copy) NSMutableAttributedString *mutableAttri;
@property(nonatomic, strong) NSMutableAttributedString *mutableStrongAttri;
NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:@"我是富文本我是富文本我是富文本我是富文本"];
[attriStr addAttributes:@{NSForegroundColorAttributeName: UIColor.redColor, NSFontAttributeName: [UIFont systemFontOfSize:16]} range:NSMakeRange(0, attriStr.length)];
[attriStr addAttributes:@{NSForegroundColorAttributeName: UIColor.blackColor, NSFontAttributeName: [UIFont systemFontOfSize:20]} range:NSMakeRange(2, 4)];
self.mutableAttri = attriStr; // _mutableAttri类型是NSConcreteAttributedString
self.mutableStrongAttri = attriStr; // _mutableStrongAttri类型是NSConcreteMutableAttributedString
NSLog(@"attriStr: %@, %p", attriStr, attriStr);
NSLog(@"mutableCopyAttri: %@, %p", self.mutableAttri, self.mutableAttri);
NSLog(@"mutableStrongAttri: %@, %p", self.mutableStrongAttri, self.mutableStrongAttri);
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
NSAttributedString *tempAttr = [[NSAttributedString alloc] initWithString:@"---我是新添加的"];
// [_mutableAttri appendAttributedString:tempAttr]; // 会crash 由于copy修饰 运行时是不可变对象 不能添加字符串
[_mutableStrongAttri appendAttributedString:tempAttr];
NSLog(@"attriStr: %@, %p, %@", attriStr.string, attriStr, [attriStr class]);
NSLog(@"mutableStrongAttri: %@, %p, %@", self.mutableStrongAttri.string, self.mutableStrongAttri, [_mutableStrongAttri class]);
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的对象
@property (nonatomic, copy) NSArray *persons;
@property (nonatomic, strong) NSArray *students;
NSArray *arr = @[@"1", @"2", @"3"];
self.persons = arr;// copy修饰的_persons: 是不可变数组**__NSArrayI,_persons指向的是不可变数组arr地址**
self.students = arr; // strong修饰的_students: 是不可变数组**__NSArrayI,_students指向的是不可变数组arr地址**
NSLog(@"arr: %@, %p", arr, arr);
NSLog(@"persons: %@, %p", self.persons, self.persons);
NSLog(@"students: %@, %p", self.students, self.students);
[_students arrayByAddingObject:@"5"]; // 虽然是不可变数组__NSArrayI,arrayByAddingObject是NSArray扩展方法
NSLog(@"arr: %@, %p", arr, arr);
NSLog(@"students: %@, %p, %@", self.students, self.students, [_students class]);
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的对象
@property (nonatomic, copy) NSMutableArray *cars;
@property (nonatomic, strong) NSMutableArray *dogs;
self.cars = arr; // copy修饰的_cars: 是不可变数组__NSArrayI,_cars指向的是不可变数组arr地址,
self.dogs = arr; // strong修饰的_dogs: 是不可变数组**__NSArrayI, _dogs指向的是不可变数组arr地址。
NSLog(@"arr: %@, %p", arr, arr);
NSLog(@"cars: %@, %p, %@", self.cars, self.cars, [_cars class]);
NSLog(@"dogs: %@, %p, %@", self.dogs, self.dogs, [_dogs class]);
//[self.cars addObject:@"4"]; // 会crash cars 还是指向的arr的内存地址,仍然是不可变数组__NSArrayI
//[self.dogs addObject:@"5"]; // 会crash dogs 还是指向的arr的内存地址,仍然是不可变数组__NSArrayI
**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
@property (nonatomic, copy) NSMutableArray *cars;
@property (nonatomic, strong) NSMutableArray *dogs;
NSMutableArray *arr2 = @[@"2", @"4", @"10"].mutableCopy;
self.cars = arr2; // copy修饰的_cars: 是不可变数组__NSArrayI
self.dogs = arr2;// strong修饰的_dogs: 是可变数组__NSArrayM
// [_cars addObject:@"11"]; // 会crash,因为copy修饰 本质是不可变数组对象__NSArrayI
[_dogs addObject:@"12"];
NSLog(@"arr2: %@, %p", arr2, arr2);
NSLog(@"cars: %@, %p, %@", self.cars, self.cars, [_cars class]);
NSLog(@"dogs: %@, %p, %@", self.dogs, self.dogs, [_dogs class]);
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对象
@property(nonatomic, copy) NSDictionary *personDic;
@property (nonatomic, strong) NSDictionary *studentDic;
NSDictionary *dic = @{@"key1": @(1), @"key2": @"value"};
self.personDic = dic; // copy修饰的_personDic: 是不可变字典__NSDictionaryI
self.studentDic = dic; //strong修饰的 _studentDic: 是不可变字典__NSDictionaryI
NSLog(@"dic: %@, %p", dic, dic);
NSLog(@"personDic: %@, %p, %@", self.personDic, self.personDic, [_personDic class]);
NSLog(@"studentDic: %@, %p, %@", self.studentDic, self.studentDic, [_studentDic class]);
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
@property (nonatomic, copy) NSMutableDictionary *carDic;
@property(nonatomic, strong) NSMutableDictionary *dogDic;
NSMutableDictionary *mDic = @{@"key1": @(10), @"key2": @"value2"}.mutableCopy;
self.carDic = mDic;// 不可变字典__NSFrozenDictionaryM
self.dogDic = mDic;
NSLog(@"mDic: %@, %p, %@", mDic, mDic, [mDic class]);
NSLog(@"carDic: %@, %p, %@", self.carDic, self.carDic, [_carDic class]);
NSLog(@"dogDic: %@, %p, %@", self.dogDic, self.dogDic, [_dogDic class]);
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
//[_carDic setObject:@"value3" forKey:@"key3"]; //运行时 是不可变字典 没有setObject方法
[_dogDic setObject:@"value4" forKey:@"key4"];
NSLog(@"mDic: %@, %p, %@", mDic, mDic, [mDic class]);
NSLog(@"carDic: %@, %p, %@", self.carDic, self.carDic, [_carDic class]);
NSLog(@"dogDic: %@, %p, %@", self.dogDic, self.dogDic, [_dogDic class]);
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
@property (nonatomic, copy) NSSet *personSet;
@property(nonatomic, strong) NSSet *studentSet;
// 一、不可变NSSet赋值给NSSet对象
NSArray *setArr = @[@"1", @"2", @"3"];
NSSet *set = [[NSSet alloc] initWithArray:setArr];
self.personSet = set; // __NSSetI
self.studentSet = set; // __NSSetI
NSLog(@"set: %@, %p, %@", set, set, [set class]);
NSLog(@"personSet: %@, %p, %@", self.personSet, self.personSet, [_personSet class]);
NSLog(@"studentSet: %@, %p, %@", self.studentSet, self.studentSet, [_studentSet class]);
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对象
@property (nonatomic, copy) NSMutableSet *carSet;
@property (nonatomic, strong) NSMutableSet *dogSet;
self.carSet = set;
self.dogSet = set;
// [_carSet addObject:@"4"]; // crash
// [_dogSet addObject:@"5"]; // crash
NSLog(@"carSet: %@, %p, %@", self.carSet, self.carSet, [_carSet class]);
NSLog(@"dogSet: %@, %p, %@", self.dogSet, self.dogSet, [_dogSet class]);
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
@property (nonatomic, copy) NSMutableSet *carSet;
@property (nonatomic, strong) NSMutableSet *dogSet;
NSArray *msetArr = @[@"1", @"2", @"3"];
NSMutableSet *mSet = [NSMutableSet setWithArray:msetArr];
self.carSet = mSet;
self.dogSet= mSet;
// [_carSet addObject:@"4"]; //运行时是 __NSSetI 无addObject方法
[_dogSet addObject:@"5"];
NSLog(@"mSet: %@, %p, %@", mSet, mSet, [mSet class]);
NSLog(@"carSet: %@, %p, %@", self.carSet, self.carSet, [_carSet class]);
NSLog(@"dogSet: %@, %p, %@", self.dogSet, self.dogSet, [_dogSet class]);
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