简体   繁体   中英

Swift: Terminating app with uncaught exception type NSException

I'm using an open source app example in my project.

When I change the Facebook credentials in info.plist, I get this " terminating with uncaught exception error ".

But when I revert to the default(as used in initial open source application), I don't get any crash/error and app works smoothly.

I can't figure out the exception from this code, can anyone help ?

@IBAction func facebookLogin(sender: UIButton) {

        PFFacebookUtils.logInWithPermissions(["public_profile", "email", "user_friends"], block: { (user: PFUser!, error: NSError!) -> Void in
            if user != nil {
                if user[PF_USER_FACEBOOKID] == nil {
                    self.requestFacebook(user)
                } else {
                    self.userLoggedIn(user)
                }
            } else {
                if error != nil {
                    println(error)
                    if let info = error.userInfo {
                        println(info)
                    }
                }
                MBProgressHUD.showError("Facebook sign in error")
            }
        })
    }

    func requestFacebook(user: PFUser) {
        var request = FBRequest.requestForMe()
        request.startWithCompletionHandler { (connection: FBRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
            if error == nil {
                var userData = result as! [String: AnyObject]!
                self.processFacebook(user, userData: userData)
            } else {
                PFUser.logOut()

            }
        }
    }

    func processFacebook(user: PFUser, userData: [String: AnyObject]) {
        let facebookUserId = userData["id"] as! String
        var link = "http://graph.facebook.com/\(facebookUserId)/picture"
        let url = NSURL(string: link)
        var request = NSURLRequest(URL: url!)
        let params = ["height": "150", "width": "150", "type": "square"]
        Alamofire.request(.GET, link, parameters: params).response() {
            (request, response, data, error) in

            if error == nil {
                var image = UIImage(data: data! as! NSData)!

                if image.size.width > 280 {
                    image = Images.resizeImage(image, width: 280, height: 280)!
                }
                var filePicture = PFFile(name: "picture.jpg", data: UIImageJPEGRepresentation(image, 0.6))
                filePicture.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
                    if error != nil {
                        println("Error saving photo")
                    }
                })

                if image.size.width > 60 {
                    image = Images.resizeImage(image, width: 60, height: 60)!
                }
                var fileThumbnail = PFFile(name: "thumbnail.jpg", data: UIImageJPEGRepresentation(image, 0.6))
                fileThumbnail.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
                    if error != nil {
                        prinln("Error saving thumbnail")
                    }
                })

                user[PF_USER_EMAILCOPY] = userData["email"]
                user[PF_USER_FULLNAME] = userData["name"]
                user[PF_USER_FULLNAME_LOWER] = (userData["name"] as! String).lowercaseString
                user[PF_USER_FACEBOOKID] = userData["id"]
                user[PF_USER_PICTURE] = filePicture
                user[PF_USER_THUMBNAIL] = fileThumbnail
                user.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError!) -> Void in
                    if error == nil {
                        self.userLoggedIn(user)
                    } else {
                        PFUser.logOut()
                        if let info = error!.userInfo {
                            ProgressHUD.showError("Login error")
                            println(info["error"] as! String)
                        }
                    }
                })
            } else {
                PFUser.logOut()
                if let info = error!.userInfo {
                    prinln("Failed to fetch Facebook photo")
                    println(info["error"] as! String)
                }
            }
        }
    }

This is what I get when the app crashes: "*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use nil for keys or values on PFObject. Use NSNull

.... libc++abi.dylib: terminating with uncaught exception of type NSException"

Just a shot in the dark: Has it something to do with the escape sequence in your link:

\\ (facebookUserId) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM