我有代码:
///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
仍为空数组。
任何想法在这里出了什么问题?
答案 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
})