ios的swift中已有一个自定义对象:
var curCustomerItem:CustomerItem class CustomerItem: NSObject{ //has got all info or not var gotAllInfo:Bool var id:String var active:Bool var type:Int var name:String var phone:String var genderIndex:Int 。。。。 } |
现在想要复制该对象的值
用:
let copiedObj = self.curCustomerItem.copy() |
直接导致崩溃:
2016-06-23 15:05:45.792 SalesApp[42331:4302380] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[SalesApp.CustomerItem copyWithZone:]: unrecognized selector sent to instance 0x7be7b180’ *** First throw call stack: ( 0 CoreFoundation 0x01e21494 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x01676e02 objc_exception_throw + 50 2 CoreFoundation 0x01e2b253 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275 3 CoreFoundation 0x01d6089d ___forwarding___ + 1037 4 CoreFoundation 0x01d6046e _CF_forwarding_prep_0 + 14 5 libobjc.A.dylib 0x0168b3cf -[NSObject copy] + 41 6 SalesApp 0x00210af9 _TFC8SalesApp28CustomerDetailViewController12editCustomerfCSo8UIButtonT_ + 873 7 SalesApp 0x0021160d _TToFC8SalesApp28CustomerDetailViewController12editCustomerfCSo8UIButtonT_ + 61 8 libobjc.A.dylib 0x0168b0b5 -[NSObject performSelector:withObject:withObject:] + 84 9 UIKit 0x02ac8e38 -[UIApplication sendAction:to:from:forEvent:] + 118 10 UIKit 0x02ac8db7 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64 11 UIKit 0x02c6cf3b -[UIControl sendAction:to:forEvent:] + 79 12 UIKit 0x02c6d2d4 -[UIControl _sendActionsForEvents:withEvent:] + 433 13 UIKit 0x02c6c2c1 -[UIControl touchesEnded:withEvent:] + 714 14 UIKit 0x0304fd2e _UIGestureRecognizerUpdate + 12763 15 UIKit 0x02b48efd -[UIWindow _sendGesturesForEvent:] + 1559 16 UIKit 0x02b4a5b6 -[UIWindow sendEvent:] + 1137 17 UIKit 0x02aebbe8 -[UIApplication sendEvent:] + 266 18 UIKit 0x02ac0769 _UIApplicationHandleEventQueue + 7795 19 CoreFoundation 0x01d33e5f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 20 CoreFoundation 0x01d29aeb __CFRunLoopDoSources0 + 523 21 CoreFoundation 0x01d28f08 __CFRunLoopRun + 1032 22 CoreFoundation 0x01d28846 CFRunLoopRunSpecific + 470 23 CoreFoundation 0x01d2865b CFRunLoopRunInMode + 123 24 GraphicsServices 0x063d2664 GSEventRunModal + 192 25 GraphicsServices 0x063d24a1 GSEventRun + 104 26 UIKit 0x02ac6eb9 UIApplicationMain + 160 27 SalesApp 0x001a33b1 main + 145 28 libdyld.dylib 0x04627a25 start + 1 29 ??? 0x00000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException |
但是之前看定义:
@available(iOS 2.0, *) public class NSObject : NSObjectProtocol { public class func load() public class func initialize() public init() public func finalize() public func copy() -> AnyObject public func mutableCopy() -> AnyObject |
貌似应该是可以的才对啊。
swift copy object value
How to copy objects in Swift using copy() – Swift 2 example code
Implementing copy() in Swift – Stack Overflow
Swift 值类型,引用类型,深拷贝,浅拷贝,Copy,MutableCopy – Leo的专栏 – 博客频道 – CSDN.NET
-》原来是需要自己去手动实现自己的类的copy函数的。。。
最后,写了代码去copy,结果调用的时候,还是麻烦,
所以最后干脆去掉copy,直接加了个从已有类,去初始化得到新的instance的值:
init(curCustomerItem:CustomerItem) { self.gotAllInfo = curCustomerItem.gotAllInfo self.id = curCustomerItem.id self.active = curCustomerItem.active self.type = curCustomerItem.type self.name = curCustomerItem.name self.phone = curCustomerItem.phone self.genderIndex = curCustomerItem.genderIndex 。。。 } // @nonobjc func copy() -> CustomerItem { // return CustomerItem(curCustomerItem: self) // } |
调用方式:
let copiedCustomerItem = CustomerItem(curCustomerItem: self.curCustomerItem) |
即可。
转载请注明:在路上 » [基本解决]swift中如何复制一个自定义对象的值