-
copy
- 只會(huì)產(chǎn)生不可變的副本對(duì)象(比如NSString)
-
mutableCopy
- 只會(huì)產(chǎn)生可變的副本對(duì)象(比如NSMutableString)
ios 中并不是所有的對(duì)象都支持copy,mutableCopy,遵守NSCopying協(xié)議的類可以發(fā)送copy消息,遵守NSMutableCopying協(xié)議的類才可以發(fā)送mutableCopy消息。
Foundation框架支持復(fù)制的對(duì)象有
NSString、NSArray、NSNumber、NSDictionary、NSMutableString、NSMutableArray、NSMutableDictionary
等。假如發(fā)送了一個(gè)沒有遵守上訴兩協(xié)議而發(fā)送copy或者 mutableCopy,那么就會(huì)發(fā)生異常。但是默認(rèn)的ios類并沒有遵守這兩個(gè)協(xié)議。
如果想自定義一下copy 那么就必須遵守NSCopying,并且實(shí)現(xiàn) copyWithZone: 方法,如果想自定義一下mutableCopy 那么就必須遵守NSMutableCopying,并且實(shí)現(xiàn) mutableCopyWithZone: 方法。
@interface DBConfiguration : NSObject <NSCopying>
/** 包間配置 */
@property (nonatomic, strong) NSArray *roomConfigure;
/** 宴會(huì)類型 */
@property (nonatomic, strong) NSArray *banquetCategory;
/** 包間人數(shù) */
@property (nonatomic, assign) NSInteger peopleNum;
/** 店鋪配置 */
@property (nonatomic, strong) NSArray *shopConfigure;
@end
@implementation DBConfiguration
- (id)copyWithZone:(NSZone *)zone
{
DBConfiguration *copy = [[DBConfiguration allocWithZone:zone] init];
copy.roomConfigure = [self.roomConfigure copy];
copy.banquetCategory = [self.banquetCategory copy];
copy.peopleNum = self.peopleNum;
copy.shopConfigure = [self.shopConfigure copy];
return copy;
}
@end