[英]Connect Objective-C framework to Swift iOS 8 app (Parse framework)
[英]Parse Objective - C to Swift iOS
我在尝试将Objective-C代码更改为Swift时遇到麻烦。 这是解析框架。 如果有人知道如何用Swift编写以下代码,那将对我有很大帮助。
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
//The registration was successful, go to the wall
[self performSegueWithIdentifier:@"SignupSuccesful" sender:self];
}
[NSObject:Anyobject]? 没有名为下标成员的成员,将引发编译错误。
代码应该是这样的,因为userInfor可能为nil。
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if !(error != nil) {
// Hooray! Let them use the app now.
} else {
if let errorString = error.userInfo?["error"] as? NSString {
println(errorString)
}
}
}
分析恰好在其文档中提供了此确切方法的示例 。 我想如果我在这里摘录,他们会没事的:
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if !error {
// Hooray! Let them use the app now.
} else {
let errorString = error.userInfo["error"] as NSString
// Show the errorString somewhere and let the user try again.
}
}
就像这样。
user.signUpInBackground {
(success: Bool!, error: NSError!) -> Void in
if !error {
[unowned self] in
self.performSegue("SignupSuccesful", sender:self);
}
编辑:解析实际上具有Swift支持。 这是一个教程 。
Edit2:您不应该在块内引用self。 改用unowned self
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.