简体   繁体   English

解析目标-C到Swift iOS

[英]Parse Objective - C to Swift iOS

I am having trouble trying to change Objective - C code to Swift. 我在尝试将Objective-C代码更改为Swift时遇到麻烦。 This is with the Parse framework. 这是解析框架。 If anyone knows how the following code should be written in Swift, it would help me a lot. 如果有人知道如何用Swift编写以下代码,那将对我有很大帮助。

 [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (!error) {
        //The registration was successful, go to the wall
        [self performSegueWithIdentifier:@"SignupSuccesful" sender:self];

    }

[NSObject: Anyobject]? [NSObject:Anyobject]? does not have a member named subscript parse compile error will be thrown. 没有名为下标成员的成员,将引发编译错误。

The code should be like that because userInfor maybe be nil. 代码应该是这样的,因为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)
        }
    }
}

Parse happens to have an example of this exact method in their documentation. 分析恰好在其文档中提供了此确切方法的示例 I think they'll be OK if I excerpt it here: 我想如果我在这里摘录,他们会没事的:

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.
  }
}

It would be something like this. 就像这样。

user.signUpInBackground {
    (success: Bool!, error: NSError!) -> Void in
    if !error {
        [unowned self] in
        self.performSegue("SignupSuccesful", sender:self);
}

Edit: Parse actually has Swift support. 编辑:解析实际上具有Swift支持。 Here's a tutorial for it. 这是一个教程

Edit2: You shouldn't refer to self inside a block. Edit2:您不应该在块内引用self。 Use unowned self instead. 改用unowned self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM