Iphone 复制NSMutableArray时出现问题
我正在尝试将一个阵列复制到另一个阵列:Iphone 复制NSMutableArray时出现问题,iphone,objective-c,cocoa,cocoa-touch,arrays,Iphone,Objective C,Cocoa,Cocoa Touch,Arrays,我正在尝试将一个阵列复制到另一个阵列: NSMutableArray *itemsCopy = [[NSMutableArray alloc] initWithArray:self.items copyItems:YES]; 但我得到了一个错误: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Item copyWithZone:]: unrecognized sele
NSMutableArray *itemsCopy = [[NSMutableArray alloc] initWithArray:self.items copyItems:YES];
但我得到了一个错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Item copyWithZone:]: unrecognized selector sent to instance 0x5a74900'
*** Call stack at first throw:
(
0 CoreFoundation 0x025afc99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x026fd5de objc_exception_throw + 47
2 CoreFoundation 0x025b17ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x02521496 ___forwarding___ + 966
4 CoreFoundation 0x02521052 _CF_forwarding_prep_0 + 50
5 CoreFoundation 0x025108fa -[NSObject(NSObject) copy] + 42
6 CoreFoundation 0x025ab732 -[NSArray initWithArray:range:copyItems:] + 290
7 CoreFoundation 0x02513963 -[NSArray initWithArray:copyItems:] + 99
8 MyViewController 0x0000787d -[MyViewController tableView:didSelectRowAtIndexPath:] + 258
9 UIKit 0x003968f8 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
10 UIKit 0x0038d1de -[UITableView _userSelectRowAtIndexPath:] + 219
11 Foundation 0x000a404e __NSFireDelayedPerform + 441
12 CoreFoundation 0x025910c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
13 CoreFoundation 0x02592704 __CFRunLoopDoTimer + 1364
14 CoreFoundation 0x024ef089 __CFRunLoopRun + 1817
15 CoreFoundation 0x024ee600 CFRunLoopRunSpecific + 208
16 CoreFoundation 0x024ee521 CFRunLoopRunInMode + 97
17 GraphicsServices 0x02db52c8 GSEventRunModal + 217
18 GraphicsServices 0x02db538d GSEventRun + 115
19 UIKit 0x00332e8f UIApplicationMain + 1160
20 MyViewController 0x0000210c main + 102
21 MyViewController 0x0000209d start + 53
)
terminate called after throwing an instance of 'NSException'
您需要确保
self.items
的所有内容都采用了NSCopying协议
如果您只需要一个浅拷贝,请将-mutableCopy
消息发送到self.items
NSMutableArray *itemsCopy = [self.items mutableCopy];
您需要确保
self.items
的所有内容都采用了NSCopying协议
如果您只需要一个浅拷贝,请将-mutableCopy
消息发送到self.items
NSMutableArray *itemsCopy = [self.items mutableCopy];
如果默认情况下没有复制实现该协议的对象,则必须为类提供
copyWithZone
选择器(根据NSCopying
协议)
因此,如果要复制自定义对象,就必须实现它。
copy
方法始终调用copyWithZone
。。而且您必须始终提供实现,它自己不知道在对象内部复制什么。如果您没有复制默认实现该协议的对象,则必须为类提供copyWithZone
选择器(根据NSCopying
协议)
因此,如果要复制自定义对象,就必须实现它。
copy
方法始终调用copyWithZone
。。而且您必须始终提供实现,它自己不知道在对象内部复制什么。如果我设置copyItems:否,它不会将self.items的内容复制到新数组中吗?@Sheehan Alam:不会,但是如果您的对象不符合NSCopying协议,那无论如何都不可能。好奇,copyItems的意义是什么:如果它不将对象复制到新数组中,则为否?在什么情况下,你会复制一个数组,而不是它的内容?@Sheehan:如果你通过NO
,内容将被共享。但有时您只想在不涉及内容的情况下整体修改列表,例如,复制UIView子视图列表并对其进行排序。Sheeham Alam:copyItems:YES
复制项目,并使用副本初始化新数组,将原始项目仅保留在原始数组中(以及可能存在的其他位置)。使用copyItems:NO
,新数组被初始化为与原始数组保持相同的项。如果我设置了copyItems:NO,它不会将self.items的内容复制到新数组中吗?@Sheehan-Alam:不会,但是如果您的对象不符合NSCopying协议,那无论如何都不可能。好奇,copyItems的意义是什么:如果它不将对象复制到新数组中,则为否?在什么情况下,你会复制一个数组,而不是它的内容?@Sheehan:如果你通过NO
,内容将被共享。但有时您只想在不涉及内容的情况下整体修改列表,例如,复制UIView子视图列表并对其进行排序。Sheeham Alam:copyItems:YES
复制项目,并使用副本初始化新数组,将原始项目仅保留在原始数组中(以及可能存在的其他位置)。使用copyItems:NO
,将初始化新数组以保存与原始数组相同的项。