我正在尝试创建一个对象,并通过NSKeyedArchiver.archiveRootObject方法保存它(我知道此方法已弃用,但这是为了学校的工作),但是一旦我尝试保存,应用程序就会崩溃。我已将问题缩小为“ let isSuccessfulSave”行。
这是一个iOS应用程序,我正在当前运行iOS 12.2的iOS模拟器中运行它,我试图完全卸载该应用程序并重新安装它(认为旧的保存尝试可能是问题所在),但问题仍然存在一样。
保存功能:
@IBAction func saveImage(_ sender: Any) {
if self.myImageView.image == nil{
return
}
if nameTextField.text == ""{
return
}
let fileName = nameTextField.text ?? ""
let photo = self.myImageView.image!
let thisFile = MyFile(name: fileName, photo: photo, fileType: MyFile.FileType.drawing)//Last argument is an enum declared in the MyFile class
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(thisFile!, toFile: MyFile.ArchiveURL.path)//Last argument is declared as static let ArchiveURL = DocumentsDirectory.appendingPathComponent("myFiles") in the MyFile class
if isSuccessfulSave{
os_log("Drawing successfully saved.", log: OSLog.default, type: .debug)
} else {
os_log("Failed to save drawing", log: OSLog.default, type: .error)
}
}
正在使用的初始化:
init?(name: String, photo: UIImage, fileType: FileType){
guard !name.isEmpty else{
return nil
}
self.name = name
self.photo = photo
self.fileType = fileType
}
错误日志如下:
2019-07-15 13:25:55.935796-0700 AppleNotes[2578:45797] libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform. 2019-07-15 13:26:01.035761-0700 AppleNotes[2578:45797] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/gabriel/Library/Developer/CoreSimulator/Devices/7851257A-19F9-42EA-9E80-318C61C15ADD/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2019-07-15 13:26:01.040518-0700 AppleNotes[2578:45797] [MC] Reading from private effective user settings. /Users/gabriel/Library/Developer/CoreSimulator/Devices/7851257A-19F9-42EA-9E80-318C61C15ADD/data/Containers/Data/Application/B06CD57F-2FED-4EA2-B802-7942065E9B60/Documents/myFiles 2019-07-15 13:26:04.923588-0700 AppleNotes[2578:45797] -[__SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x60000184dbc0 2019-07-15 13:26:04.935456-0700 AppleNotes[2578:45797] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x60000184dbc0' *** First throw call stack: ( 0 CoreFoundation 0x000000010fa676fb __exceptionPreprocess + 331 1 libobjc.A.dylib 0x000000010ac1aac5 objc_exception_throw + 48 2 CoreFoundation 0x000000010fa85ab4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 CoreFoundation 0x000000010fa6c443 ___forwarding___ + 1443 4 CoreFoundation 0x000000010fa6e238 _CF_forwarding_prep_0 + 120 5 Foundation 0x000000010a6aa3d0 _encodeObject + 1230 6 AppleNotes 0x000000010a1d59db $s10AppleNotes6MyFileC6encode4withySo7NSCoderC_tF + 1259 7 AppleNotes 0x000000010a1d5afc $s10AppleNotes6MyFileC6encode4withySo7NSCoderC_tFTo + 60 8 Foundation 0x000000010a6aa3d0 _encodeObject + 1230 9 Foundation 0x000000010a6a9588 +[NSKeyedArchiver archiveRootObject:toFile:] + 214 10 AppleNotes 0x000000010a1d1b5b $s10AppleNotes11PaintWindowC9saveImageyyypF + 2763 11 AppleNotes 0x000000010a1d1f7c $s10AppleNotes11PaintWindowC9saveImageyyypFTo + 76 12 UIKitCore 0x0000000110bb3204 -[UIApplication sendAction:to:from:forEvent:] + 83 13 UIKitCore 0x000000011031d963 __45-[_UIButtonBarTargetAction _invoke:forEvent:]_block_invoke + 154 14 UIKitCore 0x000000011031d89c -[_UIButtonBarTargetAction _invoke:forEvent:] + 152 15 UIKitCore 0x0000000110bb3204 -[UIApplication sendAction:to:from:forEvent:] + 83 16 UIKitCore 0x0000000110608c19 -[UIControl sendAction:to:forEvent:] + 67 17 UIKitCore 0x0000000110608f36 -[UIControl _sendActionsForEvents:withEvent:] + 450 18 UIKitCore 0x0000000110607eec -[UIControl touchesEnded:withEvent:] + 583 19 UIKitCore 0x0000000110bebeee -[UIWindow _sendTouchesForEvent:] + 2547 20 UIKitCore 0x0000000110bed5d2 -[UIWindow sendEvent:] + 4079 21 UIKitCore 0x0000000110bcbd16 -[UIApplication sendEvent:] + 356 22 UIKitCore 0x0000000110c9c293 __dispatchPreprocessedEventFromEventQueue + 3232 23 UIKitCore 0x0000000110c9ebb9 __handleEventQueueInternal + 5911 24 CoreFoundation 0x000000010f9cebe1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 25 CoreFoundation 0x000000010f9ce463 __CFRunLoopDoSources0 + 243 26 CoreFoundation 0x000000010f9c8b1f __CFRunLoopRun + 1231 27 CoreFoundation 0x000000010f9c8302 CFRunLoopRunSpecific + 626 28 GraphicsServices 0x000000010d7fc2fe GSEventRunModal + 65 29 UIKitCore 0x0000000110bb1ba2 UIApplicationMain + 140 30 AppleNotes 0x000000010a1cbecb main + 75 31 libdyld.dylib 0x00000001132d7541 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)