在代码块中获取值以在swift中返回函数

时间:2015-09-27 14:31:47

标签: parse-platform swift2

我正在使用parse,我正在尝试返回表的row.count并将其输出到label.text。

func numberOfPostedJobsCount() -> Int{
    var postsCount: Int?
    var query = PFQuery(className: "Post")
    if let user = PFUser.currentUser() {
        query.whereKey("user", equalTo: user)
    }
    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error) -> Void in
        if error == nil {
            //query succeeded
            print("\(objects?.count)")
        } else {
            print("\(error)")
        }
    }

    return postsCount!
}

viewDidLoad()中的代码是

numberOfPostedJobs.text = String(numberOfPostedJobsCount())

错误:致命错误:在解包可选值时意外发现nil

编辑1:我尝试添加公共或私有变量,但它没有用。

func numberOfPostedJobsCount() -> (Int) {
    var num1: Int = 0
    var query = PFQuery(className: "Post")
    if let user = PFUser.currentUser() {
        query.whereKey("user", equalTo: user)
    }
    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error) -> Void in

        if error == nil {
            //query succeeded
            print("\(objects?.count)")
            num1 = (objects?.count)!
            print(num1)
        } else {
            print("\(error)")
        }
    }

    return num1
}

2 个答案:

答案 0 :(得分:1)

原因是postsCount已声明,但从未初始化,因此当你解开它时它是nil

请注意函数findObjectsInBackgroundWithBlock异步工作,这意味着在返回语句之后始终调用该块。在此块中,您必须更新数据源和/或UI。

修改

从您的函数返回Void并使用完成处理程序,例如

func numberOfPostedJobsCount(completion: (Int) -> ()) {
  var query = PFQuery(className: "Post")
  if let user = PFUser.currentUser() {
    query.whereKey("user", equalTo: user)
  }
  query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error) -> Void in

    if error == nil {
      //query succeeded
      print("\(objects?.count)")
      let num1 = (objects?.count)!
      completion(num1)
    } else {
      print("\(error)")
       completion(0)
    }
  }
}

你以这种方式调用函数

numberOfPostedJobsCount() { (postsCount) -> () in
  numberOfPostedJobs.text = String(postsCount)
}

答案 1 :(得分:0)

您可以使用信号量使异步方法同步工作:

问题陈述的示例方法

func test()->Int {
    var postsCount: Int?
    let sema:dispatch_semaphore_t = dispatch_semaphore_create(0)

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { () -> Void in
        postsCount = 100
        dispatch_semaphore_signal(sema)
    }

    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
    return postsCount!
}

以上代码:

func numberOfPostedJobsCount() -> Int{
var postsCount: Int?
var query = PFQuery(className: "Post")
let sema:dispatch_semaphore_t = dispatch_semaphore_create(0)
if let user = PFUser.currentUser() {
    query.whereKey("user", equalTo: user)
}
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error) -> Void in
    if error == nil {
        //query succeeded
        postsCount = objects?.count
        dispatch_semaphore_signal(sema)
        print("\(objects?.count)")
    } else {
        postsCount = 0
        dispatch_semaphore_signal(sema)
        print("\(error)")
    }
}
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
return postsCount!
}