标签:
Objective-c中某个实例对象是否支持copy操作,要看这个对象是否实现了NSCopying协议:
@protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end
对于mutableCopy操作对应的有NSMutableCopying协议:
@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
我们看到NSObject接口里面是有copy和mutableCopy方法的,是不是说我们上面说的必须遵循协议的说法是错误的呢?
@interface NSObject <NSObject> { Class isa OBJC_ISA_AVAILABILITY; } ........ - (id)copy; - (id)mutableCopy; ....... @end
我们来做一个测试:
//test NSObject copy NSObject *originObject = [[NSObject alloc] init]; NSObject *copiedObject = [originObject copy]; NSObject *mutableCopiedObject = [originObject mutableCopy]; NSLog(@"%@, %@, %@", originObject, copiedObject, mutableCopiedObject);
当代码执行到[originObject copy]时crash了,提示NSObject并没有实现NSCopying协议的方法,
2015-04-27 14:16:56.680 NSLockTest[52535:816047] -[NSObject copyWithZone:]: unrecognized selector sent to instance 0x79f909b0
标签:
原文地址:http://www.cnblogs.com/goodyboy6/p/4460071.html