Kevin
Kevin

Reputation: 27

Getting NSException Error Crash App

so I got an NSException error even though my code had nothing wrong. Before i getting NSException i add a fileprivate function 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)

            })
        })
    }
}

And the full error 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

There isn't any error in my code . But when i run with simulator , once it opened the app it's crashing right away Is there any solution with my problem that i have thank you

EDIT

My User class is like this:

 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
    }
}

Upvotes: 0

Views: 229

Answers (1)

vadian
vadian

Reputation: 285150

The error is not related to this function. The stack trace states that somewhere you are calling KVC method setValuesForKeys. There is the place where the error occurs.

To make a class key-value compliant you have to add the @objc dynamic attributes to each affected property

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
    }
}

Alternatively omit all @objc attributes and add @objcMembers in front of the class.

@objcMembers
class User: NSObject {
    dynamic var id: String?
    dynamic ...

Basically you are discouraged from using these KVC methods in Swift anyway.

Upvotes: 2

Related Questions