OAuthSwift 关闭捕获值不起作用
OAuthSwift closure Capturing values not working
func queryProducts() -> Array<String> {
var final = [String]()
var temp = [String]()
self.oauthswift.client.get("http://api.v3.factual.com/t/products-cpg?q=coke",
success: {
(data, response) in
let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
let json = JSON(data: data)
if json["status"].stringValue == "ok" {
// we're OK to parse!
for result in json["response"]["data"].arrayValue {
final.append(result["product_name"].stringValue)
}
}
temp = final
}
, failure: { error in
print(error)
})
return temp
}
大家好,swift 的新手,我一直在尝试从这个 successhandler 中获取值,但似乎没有任何效果。在闭包内,我可以打印我想要的数组。
最后,我试图 return 数组最后附加所有产品名称。
它应该是什么return:
["Cola Diet Plus", "Zero Sixer", "Cola Diet Sweetened With Splenda", "Cola Diet", "Coke Diet", "Cola Diet Cherry", "Cola Diet With Lemon", "Cola Diet Twin Pack", "Cola Vanilla", "Cola Diet Vanilla", "Cola Diet", "Cola Diet With Lemon", "Cola Diet Caffeine Free", "Cola Diet With Lemon", "Coca-Cola", "Cola Diet Caffeine Free", "Cola Diet With Lime"、"Fountain Syrup Diet"、"Cola Diet"、"Cola Diet With Lemon"]
它是什么 return:
[]
任何建议都会很棒!
你不能return那样。你必须使用闭包。
func queryProducts(successCallback: ([String])->Void) {
var final = [String]()
var temp = [String]()
self.oauthswift.client.get("http://api.v3.factual.com/t/products-cpg?q=coke",
success: {
(data, response) in
let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
let json = JSON(data: data)
if json["status"].stringValue == "ok" {
// we're OK to parse!
for result in json["response"]["data"].arrayValue {
final.append(result["product_name"].stringValue)
}
}
temp = final
successCallback(temp) // callback here
}
, failure: { error in
print(error)
})
}
然后你可以这样调用:
queryProducts() {
arrayProducts in
// doing something, arrayProduct is temp array
}
func queryProducts() -> Array<String> {
var final = [String]()
var temp = [String]()
self.oauthswift.client.get("http://api.v3.factual.com/t/products-cpg?q=coke",
success: {
(data, response) in
let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
let json = JSON(data: data)
if json["status"].stringValue == "ok" {
// we're OK to parse!
for result in json["response"]["data"].arrayValue {
final.append(result["product_name"].stringValue)
}
}
temp = final
}
, failure: { error in
print(error)
})
return temp
}
大家好,swift 的新手,我一直在尝试从这个 successhandler 中获取值,但似乎没有任何效果。在闭包内,我可以打印我想要的数组。
最后,我试图 return 数组最后附加所有产品名称。
它应该是什么return: ["Cola Diet Plus", "Zero Sixer", "Cola Diet Sweetened With Splenda", "Cola Diet", "Coke Diet", "Cola Diet Cherry", "Cola Diet With Lemon", "Cola Diet Twin Pack", "Cola Vanilla", "Cola Diet Vanilla", "Cola Diet", "Cola Diet With Lemon", "Cola Diet Caffeine Free", "Cola Diet With Lemon", "Coca-Cola", "Cola Diet Caffeine Free", "Cola Diet With Lime"、"Fountain Syrup Diet"、"Cola Diet"、"Cola Diet With Lemon"]
它是什么 return: []
任何建议都会很棒!
你不能return那样。你必须使用闭包。
func queryProducts(successCallback: ([String])->Void) {
var final = [String]()
var temp = [String]()
self.oauthswift.client.get("http://api.v3.factual.com/t/products-cpg?q=coke",
success: {
(data, response) in
let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
let json = JSON(data: data)
if json["status"].stringValue == "ok" {
// we're OK to parse!
for result in json["response"]["data"].arrayValue {
final.append(result["product_name"].stringValue)
}
}
temp = final
successCallback(temp) // callback here
}
, failure: { error in
print(error)
})
}
然后你可以这样调用:
queryProducts() {
arrayProducts in
// doing something, arrayProduct is temp array
}