![](/img/trans.png)
[英]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.