我在这行代码上遇到错误:
if let installation = PFInstallation.currentInstallation()
完整的代码如下。它在早期版本的Swift中运行,但是由于某种原因现在出现编译错误。任何想法是什么问题?
class func logInWithFacebook() {
PFFacebookUtils.logInWithPermissions(["public_profile"], block: {
(user: PFUser?, error: NSError?) -> Void in
if user == nil {
NSLog("The user cancelled the Facebook login (user is nil)")
} else {
NSLog("The user successfully logged in with Facebook (user is NOT nil)")
if let installation = PFInstallation.currentInstallation() { // ERROR
let acl = PFACL(user: PFUser.currentUser()!) //
acl.setPublicReadAccess(true)
acl.setWriteAccess(true, forRoleWithName: "Admin")
installation.ACL = acl
installation.saveEventually()
}
// THEN I GET THE USERNAME AND fbId
Utils.obtainUserNameAndFbId()
}
})
}
最佳答案
这意味着如果PFInstallation.currentInstallation()
的返回类型必须在if let
声明的行中展开,则必须为可选。
现在,该调用返回一个PFInstallation
对象(不是PFInstallation?
,是一个Optional(PFInstallation)
)。如果这是“之前”的工作,也许您是指在Swift 1.2之前对Objective C互操作性进行了一些更改。
请参阅https://parse.com/docs/ios/api/Classes/PFInstallation.html#//api/name/currentInstallation上的文档
要更正您的代码,请删除if let
行:
class func logInWithFacebook() {
PFFacebookUtils.logInWithPermissions(["public_profile"], block: {
(user: PFUser?, error: NSError?) -> Void in
if user == nil {
NSLog("The user cancelled the Facebook login (user is nil)")
} else {
NSLog("The user successfully logged in with Facebook (user is NOT nil)")
let acl = PFACL(user: PFUser.currentUser()!) //
acl.setPublicReadAccess(true)
acl.setWriteAccess(true, forRoleWithName: "Admin")
installation.ACL = acl
installation.saveEventually()
// THEN I GET THE USERNAME AND fbId
Utils.obtainUserNameAndFbId()
}
})
}
关于xcode - Swift:条件绑定(bind)中的绑定(bind)值必须是 optional ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31454153/