Swift + Parse:'不能在PFObject上使用nil作为键或值。使用NSNull表示值。'

时间:2016-04-15 10:28:20

标签: xcode swift parse-platform

所以我有一个正在处理的Swift应用程序(在Xcode 7.3中)。

它使用Parse并且在其中我有一个生成标题中所述错误消息的函数。

代码如下所示;

@IBAction func callUber(sender: AnyObject) {

    let riderRequest = PFObject(className:"riderRequest")
    riderRequest["username"] = PFUser.currentUser()?.username
    riderRequest["location "] = PFGeoPoint(latitude:latitude, longitude:longitude)

    riderRequest.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {

            self.callUberButton.setTitle("Cancel Uber", forState: UIControlState.Normal)

        } else {

            let alert = UIAlertController(title: "Could not call Uber", message: "Please try again", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)


        }
    }


}

现在这个代码中的某个地方nil正在应用于一个值,这会导致错误消息(或者我认为)。我一直在寻找解决这个问题的方法,但是我找不到任何类似的线程来处理这个错误消息和Swift,只有Objective-C。

我猜测我必须引入某种错误处理或检查我确定对象的值不是nil,但是我似乎无法弄清楚在哪里或如何应用它。

只有按下" callUber"才会触发错误。按钮,调试控制台的完整输出如下所示。

2016-04-15 12:21:01.900 ParseStarterProject-Swift[8781:2591354] ***             Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use nil for keys or values on PFObject. Use NSNull for values.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010ca98d85 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010eca0deb objc_exception_throw + 48
2   CoreFoundation                      0x000000010ca98cbd +[NSException raise:format:] + 205
3   ParseStarterProject-Swift           0x000000010c1042a8 -[PFObject(Private) _setObject:forKey:onlyIfDifferent:] + 122
4   ParseStarterProject-Swift           0x000000010c107d01 -[PFObject setObject:forKey:] + 53
5   ParseStarterProject-Swift           0x000000010c107d46 -[PFObject setObject:forKeyedSubscript:] + 50
6   ParseStarterProject-Swift           0x000000010c0c847f _TFC25ParseStarterProject_Swift19RiderViewController8callUberfPs9AnyObject_T_ + 671
7   ParseStarterProject-Swift           0x000000010c0c89d6 _TToFC25ParseStarterProject_Swift19RiderViewController8callUberfPs9AnyObject_T_ + 54
8   UIKit                               0x000000010d726a8d -[UIApplication sendAction:to:from:forEvent:] + 92
9   UIKit                               0x000000010d899e67 -[UIControl sendAction:to:forEvent:] + 67
10  UIKit                               0x000000010d89a143 -[UIControl _sendActionsForEvents:withEvent:] + 327
11  UIKit                               0x000000010d899263 -[UIControl touchesEnded:withEvent:] + 601
12  UIKit                               0x000000010d79999f -[UIWindow _sendTouchesForEvent:] + 835
13  UIKit                               0x000000010d79a6d4 -[UIWindow sendEvent:] + 865
14  UIKit                               0x000000010d745dc6 -[UIApplication sendEvent:] + 263
15  UIKit                               0x000000011dfe0b15 -[UIApplicationAccessibility sendEvent:] + 77
16  UIKit                               0x000000010d71f553 _UIApplicationHandleEventQueue + 6660
17  CoreFoundation                      0x000000010c9be301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18  CoreFoundation                      0x000000010c9b422c __CFRunLoopDoSources0 + 556
19  CoreFoundation                      0x000000010c9b36e3 __CFRunLoopRun + 867
20  CoreFoundation                      0x000000010c9b30f8 CFRunLoopRunSpecific + 488
21  GraphicsServices                    0x0000000111eb0ad2 GSEventRunModal + 161
22  UIKit                               0x000000010d724f09 UIApplicationMain + 171
23  ParseStarterProject-Swift           0x000000010c0cc712 main + 114
24  libdyld.dylib                       0x000000010f7a792d start + 1
25  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:0)

可能是一个问题:

仔细检查你的班级名称是" riderRequest",我认为" RiderRequest"可能是实际的名字。

其次是通过转到解析仪表板并查看你的班级来检查所有的密钥名称是否正确

最后一件事是确认在设置之前,用户名,纬度和经度等所有值都不为零。

也许试试:

if let username = PFUser.currentUser()?.username && latitude != nil && longitude != nil {

    riderRequest.setObject(username, forKey: "username")
    riderRequest.setObject(PFGeoPoint(latitude: latitude, longitude: longitude), forKey: "location")

//then run the rest of the code as you have been
}