所以即使我的代码没有错,我也收到了NSException错误。在我获得NSException之前,我添加了一个文件私有函数here
fileprivate func uploadToFirebaseStorageUsingImage(image: UIImage) {
let imageName = UUID().uuidString
let ref = Storage.storage().reference().child("message_images").child("\(imageName).jpg")
if let uploadData = UIImageJPEGRepresentation(image, 0.2) {
ref.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil {
print("Failed to upload image:", error!)
return
}
ref.downloadURL(completion: { (url, error) in
guard let downloadURL = url else{
print("an error occured")
return
}
let imageUrl = downloadURL.absoluteString
self.sendMessageWithImageUrl(imageUrl, image: image)
})
})
}
}
完整错误here
2018-08-23 14:48:04.985710+0700 nextstore[33702:369585] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<nextstore.User 0x60800011f920> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000109d251e6 __exceptionPreprocess + 294
1 libobjc.A.dylib 0x00000001093ba031 objc_exception_throw + 48
2 CoreFoundation 0x0000000109d250b9 -[NSException raise] + 9
3 Foundation 0x00000001086c8b47 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
4 Foundation 0x0000000108726c02 -[NSObject(NSKeyValueCoding) setValuesForKeysWithDictionary:] + 283
5 nextstore 0x000000010724da39 _T09nextstore18MessagesControllerC28fetchUserAndSetupNavBarTitleyyFySo12DataSnapshotCcfU_ + 713
6 nextstore 0x000000010724db1d _T09nextstore18MessagesControllerC28fetchUserAndSetupNavBarTitleyyFySo12DataSnapshotCcfU_TA + 13
7 nextstore 0x00000001072338d2 _T0So12DataSnapshotCIegx_ABIeyBy_TR + 66
8 nextstore 0x000000010730d0e6 __71-[FIRDatabaseQuery observeSingleEventOfType:withBlock:withCancelBlock:]_block_invoke + 118
9 nextstore 0x000000010730d5c8 __92-[FIRDatabaseQuery observeSingleEventOfType:andPreviousSiblingKeyWithBlock:withCancelBlock:]_block_invoke + 184
10 nextstore 0x00000001072ebc1a __43-[FChildEventRegistration fireEvent:queue:]_block_invoke.68 + 122
11 libdispatch.dylib 0x000000010da737ab _dispatch_call_block_and_release + 12
12 libdispatch.dylib 0x000000010da747ec _dispatch_client_callout + 8
13 libdispatch.dylib 0x000000010da7f8cf _dispatch_main_queue_callback_4CF + 628
14 CoreFoundation 0x0000000109ce7c99 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
15 CoreFoundation 0x0000000109cabea6 __CFRunLoopRun + 2342
16 CoreFoundation 0x0000000109cab30b CFRunLoopRunSpecific + 635
17 GraphicsServices 0x000000010ef06a73 GSEventRunModal + 62
18 UIKit 0x000000010ad94057 UIApplicationMain + 159
19 nextstore 0x000000010726e5c7 main + 55
20 libdyld.dylib 0x000000010daf1955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我的代码没有任何错误。但是当我使用模拟器运行时,一旦打开应用程序,它将立即崩溃 我的问题有什么解决办法吗,谢谢你
编辑
我的用户类是这样的:
class User: NSObject {
var id: String?
var name: String?
var email: String?
var profileImageUrl: String?
init(dictionary: [String: AnyObject]) {
self.id = dictionary["id"] as? String
self.name = dictionary["name"] as? String
self.email = dictionary["email"] as? String
self.profileImageUrl = dictionary["profileImageUrl"] as? String
}
}
答案 0 :(得分:2)
该错误与该功能无关。堆栈跟踪表明您正在某个地方调用KVC方法setValuesForKeys
。在错误发生的地方。
要使类键值兼容,必须将@objc dynamic
属性添加到每个受影响的属性
class User: NSObject {
@objc dynamic var id: String?
@objc dynamic var name: String?
@objc dynamic var email: String?
@objc dynamic var profileImageUrl: String?
init(dictionary: [String: AnyObject]) {
self.id = dictionary["id"] as? String
self.name = dictionary["name"] as? String
self.email = dictionary["email"] as? String
self.profileImageUrl = dictionary["profileImageUrl"] as? String
}
}
或者忽略所有@objc
属性,并在类前面添加@objcMembers
。
@objcMembers
class User: NSObject {
dynamic var id: String?
dynamic ...
基本上,我们还是不鼓励在Swift中使用这些KVC方法。