价值不被复制

时间:2014-10-23 04:57:59

标签: ios swift

我有代码:

///Get the timeline of the logged in user.
static func GetTimeline(_count: Int) ->  [JSONValue]
{
    var tweets : [JSONValue] = []
    var count = _count

  account.getStatusesHomeTimelineWithCount(_count, sinceID: nil, maxID: nil, trimUser: true, contributorDetails: false, includeEntities: true,
    success: { (statuses) -> Void in
        tweets = statuses!

  }, nil)

    return tweets
}

我正在尝试复制此处的值:tweets = statuses!

此方法(GetTimeline)每次都返回一个空数组。通过调试和断点,我知道statuses包含值,但由于某种原因,此行tweets = statuses!无效,因此tweets仍为空数组。

任何想法在这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

我猜getStatusesHomeTimelineWithCount是非阻塞调用,success已经返回空数组后执行GetTimeline回调。

对于非阻塞调用,您不能使用返回值,但您可以使用竞争处理程序。

static func GetTimeline(_count: Int, competition: (tweets: [JSONValue]) -> ())
{
    var tweets : [JSONValue] = []
    var count = _count

    account.getStatusesHomeTimelineWithCount(_count, sinceID: nil, maxID: nil, trimUser: true, contributorDetails: false, includeEntities: true,
    success: { (statuses) -> Void in
        competition(tweets: statuses!)
    }, nil)
}

这就是您使用当前示例的方式:

let tweets = SomeObject.GetTimeline(10)
// do something with tweets

完成后,您可以执行以下操作:

SomeObject.GetTimeline(10, { (tweets) in
    // do something with tweets
})