gpt4 book ai didi

xcode - Swift + Parse : 'Can' t use nil for keys or values on PFObject. 使用 NSNull 作为值。

转载 作者:行者123 更新时间:2023-11-30 13:30:16 24 4
gpt4 key购买 nike

所以我正在开发一个 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。

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

仅当我按下“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

最佳答案

可能存在以下几个问题:

仔细检查您的类名称是否为“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
}

关于xcode - Swift + Parse : 'Can' t use nil for keys or values on PFObject. 使用 NSNull 作为值。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36644672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com