我已经按照这个将你的swift应用程序连接到Parse.com教程:http://ios-blog.co.uk/tutorials/connect-your-swift-application-to-parse-com/并且效果很好。本教程仅向您介绍如何在Parse.com服务上放置数据。所以我做了一些关于如何检索数据的研究。
在第一篇教程中,它表示将数据添加到Parse中,如下所示:
Parse.setApplicationId("your_application_key", clientKey: "your_client_key")
var object = PFObject(className: "testDataClass")
object.addObject("iOSBlog", forKey: "websiteUrl")
object.addObject("Five", forKey: "websiteRating")
object.save()
在第二篇教程中,由Parse团队编写(http://blog.parse.com/2014/06/06/building-apps-with-parse-and-swift/)它写的是这样写的:
var gameScore = PFObject(className: "GameScore")
gameScore.setObject(1337, forKey: "score")
gameScore.setObject("Sean Plott", forKey: "playerName")
gameScore.saveInBackgroundWithBlock {
(success: Bool!, error: NSError!) -> Void in
if success {
NSLog("Object created with id: \(gameScore.objectId)")
} else {
NSLog("%@", error)
}
}
然后要检索它,请使用以下代码:
var query = PFQuery(className: "GameScore")
query.getObjectInBackgroundWithId(gameScore.objectId) {
(scoreAgain: PFObject!, error: NSError!) -> Void in
if !error {
NSLog("%@", scoreAgain.objectForKey("playerName") as NSString)
} else {
NSLog("%@", error)
}
}
但是,当我使用官方解析代码时,我收到以下错误:
fatal error: Can't unwrap Optional.None
(lldb)
所有这些事情几乎发生在Xcode的每个窗格中:
有人可以告诉我有什么问题以及如何解决这个问题?我对Xcode和Swift相对较新,所以我非常感谢外行人的条款回答。
编辑这是appDelegate.swift函数
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Connect to Parse using the keys provided
Parse.setApplicationId("xxx", clientKey: "xxx")
// Store Data to Parse
var gameScore = PFObject(className: "GameScore")
gameScore.setObject(1337, forKey: "score")
gameScore.setObject("Sean Plott", forKey: "playerName")
gameScore.saveInBackgroundWithBlock {
(success: Bool!, error: NSError!) -> Void in
if success {
NSLog("Object created with id: \(gameScore.objectId)")
} else {
NSLog("%@", error)
}
}
var query = PFQuery(className: "GameScore")
query.getObjectInBackgroundWithId(gameScore.objectId) {
(scoreAgain: PFObject!, error: NSError!) -> Void in
if !error {
NSLog("%@", scoreAgain.objectForKey("playerName") as NSString)
} else {
NSLog("%@", error)
}
}
// Override point for customization after application launch.
return true
}
非常感谢。
为用户添加了新代码以查看:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Connect to Parse using the keys provided
Parse.setApplicationId("xxxx", clientKey: "xxxx")
// Store Data to Parse
var gameScore = PFObject(className: "GameScore")
gameScore.setObject(1337, forKey: "score")
gameScore.setObject("Sean Plott", forKey: "playerName")
gameScore.saveInBackgroundWithBlock {
(success: Bool!, error: NSError!) -> Void in
if (success ?? false) {
NSLog("Object created with id: \(gameScore.objectId)")
} else {
NSLog("%@", error)
}
}
var query = PFQuery(className: "GameScore")
query.getObjectInBackgroundWithId(gameScore.objectId) {
(scoreAgain: PFObject!, error: NSError!) -> Void in
if error == nil {
NSLog("%@", scoreAgain.objectForKey("playerName") as NSString)
} else {
NSLog("%@", error)
}
}
// Override point for customization after application launch.
return true
}
答案 0 :(得分:1)
这个Parse示例是在Swift处于Beta版时于6月份编写的。斯威夫特继续发展,所以他们正在做的一些事情在斯威夫特不再合法。
首先,您无法再使用error
检查可选变量nil
是否为if !error
。相反,您必须明确检查nil
,如下所示:if error == nil
。
其次,success
被声明为隐式解包的可选项。使用true
检查if success
是否有效无效。您可以执行if success == true
但如果success
为nil
,则会崩溃。相反,你可以做if (success ?? false)
。如果Bool
有值,则使用 nil coalescing operator 安全地展开可选false
,如果Bool
为nil
,则使用if
。无论哪种方式,Bool
只有在true
为{{1}}时才会成功。