溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

淺談iOS中幾個常用協(xié)議 NSCopying/NSMutableCopying

發(fā)布時間:2020-09-27 06:37:26 來源:腳本之家 閱讀:363 作者:小洋子 欄目:移動開發(fā)

1、幾點說明

說到NSCopying和NSMutableCopying協(xié)議,不得不說的就是copy和mutableCopy。

如果類想要支持copy操作,則必須實現(xiàn)NSCopying協(xié)議,也就是說實現(xiàn)copyWithZone方法;

如果類想要支持mutableCopy操作,則必須實現(xiàn)NSMutableCopying協(xié)議,也就是說實現(xiàn)mutableCopyWithZone方法;

iOS系統(tǒng)中的一些類已經(jīng)實現(xiàn)了NSCopying或者NSMutableCopying協(xié)議的方法,如果向未實現(xiàn)相應(yīng)方法的系統(tǒng)類或者自定義類發(fā)送copy或者mutableCopy消息,則會crash。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Person copyWithZone:]: unrecognized selector sent to instance 0x6080000314c0'

發(fā)送copy和mutableCopy消息,均是進(jìn)行拷貝操作,但是對不可變對象的非容器類、可變對象的非容器類、可變對象的容器類、不可變對象的容器類中復(fù)制的方式略有不同;但如下兩點是相同的:

發(fā)送copy消息,拷貝出來的是不可變對象;

發(fā)送mutableCopy消息,拷貝出來的是可變對象;

故如下的操作會導(dǎo)致crash

NSMutableString *test1 = [[NSMutableString alloc]initWithString:@"11111"];
NSMutableString *test2 = [test1 copy];
[test2 appendString:@"22222"];

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString appendString:]: unrecognized selector sent to

2、系統(tǒng)非容器類

系統(tǒng)提供的非容器類中,如NSString,NSMutableString,有如下特性:

向不可變對象發(fā)送copy,進(jìn)行的是指針拷貝;向不可變對象發(fā)送mutalbeCopy消息,進(jìn)行的是內(nèi)容拷貝;

NSString *test3 = @"111111";
NSString *test4 = [test3 copy];
NSMutableString *test5 = [test3 mutableCopy];
NSLog(@"test3 is %p, test4 is %p, tast5 is %p",test3,test4,test5);
test3 is 0x10d6bb3a8, test4 is 0x10d6bb3a8, tast5 is 0x600000073e80

向可變對象發(fā)送copy和mutableCopy消息,均是深拷貝,也就是說內(nèi)容拷貝;

NSMutableString *test11 = [[NSMutableString alloc]initWithString:@"444444"];
NSString *test12 = [test11 copy]; 
NSMutableString *test13 = [test11 mutableCopy]; 
NSLog(@"test11 is %p, test12 is %p, tast13 is %p",test11,test12,test13);
 
test11 is 0x600000073e00, test12 is 0xa003434343434346, tast13 is 0x600000073dc0

3、系統(tǒng)容器類

系統(tǒng)提供的容器類中,如NSArray,NSDictionary,有如下特性:

不可變對象copy,是淺拷貝,也就是說指針復(fù)制;發(fā)送mutableCopy,是深復(fù)制,也就是說內(nèi)容復(fù)制;

NSArray *array = [NSArray arrayWithObjects:@"1", nil];
NSArray *copyArray = [array copy];
NSMutableArray *mutableCopyArray = [array mutableCopy];
NSLog(@"array is %p, copyArray is %p, mutableCopyArray is %p", array, copyArray, mutableCopyArray);
array is 0x60800001e580, copyArray is 0x60800001e580, mutableCopyArray is 0x608000046ea0

可變對象copy和mutableCopy均是單層深拷貝,也就是說單層的內(nèi)容拷貝;

NSMutableArray *element = [NSMutableArray arrayWithObject:@1];
NSMutableArray *array = [NSMutableArray arrayWithObject:element];
NSArray *copyArray = [array copy];
NSMutableArray *mutableCopyArray = [array mutableCopy];
NSLog(@"array is %p, copyArray is %p, mutableCopyArray is %p", array, copyArray, mutableCopyArray);
[mutableCopyArray[0] addObject:@2];
NSLog(@"element is %@, array is %@, copyArray is %@, mutableCopyArray is %@", element,array,copyArray, mutableCopyArray);
 
2017-02-22 11:53:25.286 test[91520:3915695] array is 0x600000057670, copyArray is 0x600000000bc0, mutableCopyArray is 0x6080000582a0
2017-02-22 11:53:25.287 test[91520:3915695] element is (
1,
2
), array is (
 (
 1,
 2
)
), copyArray is (
 (
 1,
 2
)
), mutableCopyArray is (
 (
 1,
 2
)
)

4、自定義的類

重要說明:

1、所以的代碼設(shè)計均是針對業(yè)務(wù)需求。

2、對于自定義的類,決定能否向?qū)ο蟀l(fā)送copy和mutableCopy消息也是如此;

1、@property 聲明中用 copy 修飾

不得不說下copy和strong在復(fù)制時候的區(qū)別,此處不講引用計數(shù)的問題。

copy:拷貝一份不可變副本賦值給屬性;所以當(dāng)原對象值變化時,屬性值不會變化;

strong:有可能指向一個可變對象,如果這個可變對象在外部被修改了,那么會影響該屬性;

@interface Person : NSObject 
@property (nonatomic, copy) NSString *familyname;
@property (nonatomic, strong) NSString *nickname;
@end
Person *p1 = [[Person alloc]init];
 
NSMutableString *familyname = [[NSMutableString alloc]initWithString:@"張三"];
p1.familyname = familyname;
[familyname appendString:@"峰"];
 
NSLog(@"p1.familyname is %@",p1.familyname);
 
NSMutableString *nickname = [[NSMutableString alloc]initWithString:@"二狗"];
p1.nickname = nickname;
[nickname appendString:@"蛋兒"];
 
NSLog(@"p1.nickname is %@", p1.nickname);
2017-02-22 13:53:58.979 test[98299:3978965] p1.familyname is 張三
2017-02-22 13:53:58.979 test[98299:3978965] p1.nickname is 二狗蛋兒

2、類的對象的copy

此處唯一需要說明的一點就是注意類的繼承。

這篇文章有非常清晰詳細(xì)的說明,此處只照搬下結(jié)論:

1 類直接繼承自NSObject,無需調(diào)用[super copyWithZone:zone]

2 父類實現(xiàn)了copy協(xié)議,子類也實現(xiàn)了copy協(xié)議,子類需要調(diào)用[super copyWithZone:zone]

3 父類沒有實現(xiàn)copy協(xié)議,子類實現(xiàn)了copy協(xié)議,子類無需調(diào)用[super copyWithZone:zone]

4、copyWithZone方法中要調(diào)用[[[self class] alloc] init]來分配內(nèi)存

5、NSCopying

NSCopying是對象拷貝的協(xié)議。

類的對象如果支持拷貝,該類應(yīng)遵守并實現(xiàn)NSCopying協(xié)議。

NSCopying協(xié)議中的方法只有一個,如下:
- (id)copyWithZone:(NSZone *)zone { 
 Person *model = [[[self class] allocWithZone:zone] init];
 model.firstName = self.firstName;
 model.lastName = self.lastName;
 //未公開的成員
 model->_nickName = _nickName;
 return model;
}

3、NSMutableCopying

當(dāng)自定義的類有一個屬性是可變對象時,對此屬性復(fù)制時要執(zhí)行mutableCopyWithZone操作。

- (id)copyWithZone:(NSZone *)zone {
 AFHTTPRequestSerializer *serializer = [[[self class] allocWithZone:zone] init];
 serializer.mutableHTTPRequestHeaders = [self.mutableHTTPRequestHeaders mutableCopyWithZone:zone];
 serializer.queryStringSerializationStyle = self.queryStringSerializationStyle;
 serializer.queryStringSerialization = self.queryStringSerialization;
 
 return serializer;
}

以上這篇淺談iOS中幾個常用協(xié)議 NSCopying/NSMutableCopying就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI