我正在尝试使用swiftyJSON创建一个使用api构建一个足球运动员阵列的函数。每个玩家都需要一个包含bool值的数组条目,该值始终为true,以及" id"," fullname"和"照片"来自JSON数据(全部为字符串)。生成的数组必须在整个项目中可用,因为它将用于填充标签和UIImageViews。
如何通过函数返回这样的数组。以下是我能得到的:
// Celtic soccerama api
let baseURL = "https://api.soccerama.pro/v1.2/players/team/152?api_token=9b2LKV6TjmYEkzCjeB1efeWXeCRcNAGXkph1CjzmWYMKs4F3a1KrWh9paXIa"
struct global {
static var celticData : [(Bool, String, String, String)] = []
}
// JSON all Celtic players
func getData() -> [(Bool, String, String, String)]? {
var data : Array = [(Bool, String, String, String)]()
let url = NSURL(string: baseURL)
let request = NSURLRequest(URL: url!)
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if error == nil {
// Get the data returned from NSURLRequest
let swiftyJSON = JSON(data: data!)
// Loop through "data" array and pull out "id", "fullname" and "photo"
// Won't work:
//for i in 0 ... swiftyJSON["data"].count {
// Have to manually count
for i in 0 ... 10 {
// Add bool (true every time)
let t = true
data[i].0 = t
// Get data and append at each position in array
let id = swiftyJSON["data"][i]["id"].stringValue
let fullName = swiftyJSON["data"][i]["fullname"].stringValue
// Get image data for display and append to array
let imageArrayPosition = swiftyJSON["data"][i]["photo"].stringValue
let imageURL = NSURL(string: imageArrayPosition)
}
} else {
print("There was an error")
}
}
task.resume()
}