FB / Swift:如何通过FBSDKGraphRequest中的for循环追加一个数组? [英] FB/Swift: How to append an array via a for-loop in FBSDKGraphRequest?
问题描述
在下面的代码中,我在通过异步调用中的循环的 更新:这是我编辑代码的方式。 更新#2:我找不到观察员在这个代码中只打印一次。我已经把 FBSDKGraphRequest是异步的,所以我当你的程序仍然在主线程中运行的时候,后台线程会发生这种情况,而后台线程回滚了println命令已经执行的数据。你的问题最好的解决方案之一是添加观察员的结果,所以你会得到通知,当它发生。将观察者添加到数组可能有一些小技巧,我希望为你保持简单,所以我建议你创建一个布尔变量来告诉你值何时完成改变。要做到这一点,您将需要创建像这样的变量 现在,每当您更改数组中的值(添加或编辑)时,您都将arrayDidChange设置为true,读取完所有内容后,将arrayDidChange设置为false。 通过这种方式,您将永远可以确保在数组中填入数组中的值。 我希望能帮到你! In the following code, I'm having trouble appending values to an array via a
Update: Here's how I edited the code.
Update #2: I can't get the observer to just print once in this code. I've moved the
The FBSDKGraphRequest is async so it will happen in the background thread while your program still running in the main thread, by the point your background thread comeback with the data the println command was already executed. One of the best solutions for your problem is to add observers for the result so you will be notified when it happens. Add observers to array can be a little bit trick and I want to keep it simple for you, so I would recommend you to create a boolean variable that tells you when the value finish change. To do so you will need to create the variable like this Now everytime you change the values (add or edit) in the array you set arrayDidChange to true and after you read it and do all you need to do you set arrayDidChange to false. This way you will always be sure you will access the values in the array after it be populate. I hope that helped you! 这篇关于FB / Swift:如何通过FBSDKGraphRequest中的for循环追加一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!循环中将值附加到数组时遇到问题。我在
graphRequest
之外声明了 numbersArray
数组,这样我就可以尝试从 for
循环,并在请求之外访问它。问题是,因为 graphRequest
是一个异步调用,所以在打印前打印Boston:\(numbersArray))
numbersArray:\(numbersArray)
。
numbersArray:\ (numbersArray)
打印每个循环后数组的值,而Boston:\(numbersArray))
打印一个空数组因为它是在 graphRequest
之前调用的。我怎样才能访问 numbersArray
其外部值 graphRequest
?
class Test {
class func test(){
var numbersArray = [String] ()
让graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath:me /,parameters:nil)
graphRequest.startWithCompletionHandler({(connection,result,error) - & b
$ b array {[1,2,3]]
$ arbay.append(number)
}
$ b println(numbersArray:\(numbersArray))
})
println(Boston:\(numbersArray) )
$ / code $ / pre
class Test {
class func test(){
var numbersArray = [String]()
$ b $ var arrayDidChange:Bool = false {
didSet {
如果arrayDidChange {
println(Boston:\(numbersArray))
$ b让graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath:me /,parameters:nil)
graphRequest.startWithCompletionHandler({连接,结果,错误) - > void
$ b var array = [1,2,3]
数组{b $ b numbersArray.append(number)
arrayDidChange = true
}
})
arrayDidChange = false
}
}
arrayDidChange
s移到了所有地方,但都没有成功。如果我将 arrayDidChange = true
语句从循环中移出循环,那么
songArray < code $是空的。
class Test {
$ b $ class func test(){
var songArray = [String]()
var testArray = [Trap Queen,Dream Lover,All Of Me]
var arrayDidChange: Int = 0 {
didSet {
如果arrayDidChange == 0 {
println(Boston:\(songArray))
}
}
}
arrayDidChange ++
let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath:me / music.listens,parameters:nil)
graphRequest.startWithCompletionHandler({ ,result,error) - >如果让data = result [data]为?NSArray {
为数据中的歌曲{
如果让songData = songs [data]为?NS字典{
如果let song = songData [song] as? NSDictionary {
如果让id = song [id] as? String {
let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath:\(id),parameters:nil)
graphRequest.startWithCompletionHandler({(connection,result,error) - >如果让title = result [title]作为?String {
如果contains(testArray,title){
songArray。附加(标题)
arrayDidChange--
}
}
})
}
}
}
}
}
})
}
}
var arrayDidChange:Bool = false {
didSet {
if arrayDidChange {
callFuncDataReadArrayHere()
}
}
}
$ b
for
loop in an asynchronous call. I'm declaring the numbersArray
array outside the graphRequest
so that I can try to append values from inside the for
loop to it and access it outside the request. Problem is, because graphRequest
is an asynchronous call, it prints "Boston: \(numbersArray)")
before printing "numbersArray: \(numbersArray")"
."numbersArray: \(numbersArray")"
prints the values of the array after each loop, while "Boston: \(numbersArray)")
prints an empty array since it was called before the graphRequest
. How can I access numbersArray
with it's values outside the graphRequest
?class Test {
class func test() {
var numbersArray = [String]()
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
var array = ["1", "2", "3"]
for number in array {
numbersArray.append(number)
}
println("numbersArray: \(numbersArray)")
})
println("Boston: \(numbersArray)")
}
}
class Test {
class func test() {
var numbersArray = [String]()
var arrayDidChange:Bool = false {
didSet {
if arrayDidChange {
println("Boston: \(numbersArray)")
}
}
}
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
var array = ["1", "2", "3"]
for number in array {
numbersArray.append(number)
arrayDidChange = true
}
})
arrayDidChange = false
}
}
arrayDidChange
s all around and have had no success. If I move the arrayDidChange = true
statement out of the for
loop, then songArray
is empty.class Test {
class func test() {
var songArray = [String]()
var testArray = ["Trap Queen", "Dream Lover", "All Of Me"]
var arrayDidChange: Int = 0 {
didSet {
if arrayDidChange == 0 {
println("Boston: \(songArray)")
}
}
}
arrayDidChange++
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/music.listens", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if let data = result["data"] as? NSArray {
for songs in data {
if let songData = songs["data"] as? NSDictionary {
if let song = songData["song"] as? NSDictionary {
if let id = song["id"] as? String {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "\(id)", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if let title = result["title"] as? String {
if contains (testArray, title) {
songArray.append(title)
arrayDidChange--
}
}
})
}
}
}
}
}
})
}
}
var arrayDidChange:Bool = false {
didSet{
if arrayDidChange{
callFuncDataReadArrayHere()
}
}
}