如何使用swift返回anyObjects的列表?

时间:2015-05-31 17:24:06

标签: ios swift parse-platform

如何返回从 Parse 检索到的对象?我在返回AnyObjectself.array列表时遇到问题。还尝试不返回列表,只在方法中指定var query = PFQuery(className:"Intervaller") query.whereKey("namn", equalTo:"elit") query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

我在getSteps()方法中使用查询检索对象:

PFObject

然后,我可以通过转换为if let list = objects as? [PFObject] { for object in list { let steps = object["steps"] as! Int var myString = String(steps) println(myString) } } 来使用同一方法中的对象列表而不会出现问题:

PFObject

要在其他方法中使用这些对象,我应该返回AnyObjectself.steps = objects!的列表吗?我已经尝试了两次都没有成功,即没有对象分配给我的新列表。

如果我尝试var steps = [AnyObject]()将列表分配给我的self.getSteps() //calling the method where the Parse query is run and self.steps are assigned to objects! if let list = self.steps as? [PFObject] { for object in list { let steps = object["steps"] as! Int var myString = String(steps) println(myString) } } ,我不确定是否有任何对象被分配到我的新列表。我没有使用此代码在for循环中获取任何值

Private Sub CommandButton1_Click()
Dim Counter As Integer
Counter = -1
Range("F5").Activate
For i = 1 To 50
x = ActiveCell(1, 1).Value
y = ActiveCell(2, 1).Value
  If x = y Then
  ActiveCell.Offset(1, 0).Select
  Counter = Counter - 1
  Else
  Call subTotal
  Counter = -1
  End If
Next i
End Sub

Sub subTotal()
ActiveCell.Offset(1, 0).Select
ActiveCell.EntireRow.Insert
ActiveCell.FormulaR1C1 = "=SUM(R[" & Counter & "]C:R[-1]C)"
ActiveCell.Offset(1, 0).Select
End Sub

什么是最佳方法,如何正确投射和返回/分配?

1 个答案:

答案 0 :(得分:1)

func getSteps() {
    var query = PFQuery(className:"Intervaller")
    query.whereKey("namn", equalTo:"elit")

    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        self.steps = objects as? [PFObject]
        // Only at this point, which will be sometime after the getSteps method has returned, will self.steps be valid
    }
    // At this point in the code the completion handler has not been called so we cannot return the list.
}

例如:

self.getSteps() //calling the method where the Parse query is runned and (self.steps is not assigned before this method returns.)
// Here self.steps is not valid, since the parse query is running in the background and the completion block will not have been executed yet.
// The following code would be best off being included in the completion handler of the parse query.
if let list = self.steps as? [PFObject] {
    for object in list {
        let steps = object["steps"] as! Int
        var myString = String(steps)
        println(myString)
    }
}

因此,你可以在getSteps()方法的完成处理程序中包含需要self.steps的代码。或者,您可以通过为getSteps方法提供从查询完成块调用的完成块来使其异步:

func getSteps(completion: (objects: [AnyObject]?, error: NSError?) -> Void) {
    var query = PFQuery(className:"Intervaller")
    query.whereKey("namn", equalTo:"elit")

    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        self.steps = objects as? [PFObject]
        // Call the getSteps method's completion handler
        completion(objects: objects, error: error)
    }
}

然后你可以打电话:

self.getSteps() { (objects: [AnyObject]?, error: NSError?) -> Void in
    if let list = self.steps as? [PFObject] {
        for object in list {
            let steps = object["steps"] as! Int
            var myString = String(steps)
            println(myString)
        }
    }
}

如果有任何需要澄清,请告诉我。