所以我得到了一个NSException错误,即使我的代码没有任何错误。在我获得 NSException 之前,我在这里添加一个文件私有函数
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)
})
})
}
}
这里的完整错误
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
}
}
该错误与此函数无关。堆栈跟踪指出您正在某处调用 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 方法。