for loop cycle 始终显示 HTTP 请求后的最后一个数组值 swift 3
for loop cycle showing always the last array value swift 3 after HTTP request
我已经使用另一个 post 解决了部分问题,所以现在我可以等待请求结束。我用 Almofire 发出了一个 http 请求并解析了生成的 JSON 文件。我认为 myGroup.notify 中的代码在请求完成后正确执行(如果我打印 allCards.count returns 正确的值)。为什么如果我把 for 循环放在里面它总是显示最后一张牌?
感谢您的帮助
import UIKit
mport SwiftyJSON
import Alamofire
class ViewController: UIViewController {
//variables Declaration
var allCard = [Card]()
let allCardsHTTP: String = "https://omgvamp-hearthstone-v1.p.mashape.com/cards?mashape-key="
override func viewDidLoad() {
super.viewDidLoad()
let myGroup = DispatchGroup()
//get HTTp request with Alamofire
myGroup.enter()
Alamofire.request(allCardsHTTP, method: .get).responseJSON {
response in
if response.result.isSuccess {
let jsonCards : JSON = JSON(response.result.value!)
print("success")
self.updateJSONCards(json: jsonCards)
}
else {
print("error")
}
myGroup.leave()
}
myGroup.notify(queue: .main) {
//Updte UI here
print(self.allCard[10].name) //This is Working
for card in self.allCard {
if card.cardsSet == "Basic" {
print(card.name)
}
} //For loop always showing last card ???
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - JSON Parsing
//Write the updateJSONCards method here:
func updateJSONCards(json: JSON) {
//create the cards
for (set, value) in json {
var card = Card()
card.cardsSet = set
if json[set].count != 0 {
for i in 0...json[set].count - 1 {
card.name = json[set][i]["name"].stringValue
allCard.append(card)
}
}
else {
print("The set \(set) has no cards")
}
}
}
}
您在错误的地方创建了 Card
实例。
在内部重复循环之前,您正在创建 one 实例并将 same 实例附加到 allCard
数组。如果 Card
是 class(引用语义)该实例的所有出现都包含相同的数据(最后一个循环项)。
将两行 放入 重复循环中。
//Write the updateJSONCards method here:
func updateJSONCards(json: JSON) {
//create the cards
for (set, value) in json {
if !json[set].isEmpty {
for i in 0..<json[set].count {
let card = Card()
card.cardsSet = set
card.name = json[set][i]["name"].stringValue
allCard.append(card)
}
}
else {
print("The set \(set) has no cards")
}
}
}
我已经使用另一个 post 解决了部分问题,所以现在我可以等待请求结束。我用 Almofire 发出了一个 http 请求并解析了生成的 JSON 文件。我认为 myGroup.notify 中的代码在请求完成后正确执行(如果我打印 allCards.count returns 正确的值)。为什么如果我把 for 循环放在里面它总是显示最后一张牌?
感谢您的帮助
import UIKit
mport SwiftyJSON
import Alamofire
class ViewController: UIViewController {
//variables Declaration
var allCard = [Card]()
let allCardsHTTP: String = "https://omgvamp-hearthstone-v1.p.mashape.com/cards?mashape-key="
override func viewDidLoad() {
super.viewDidLoad()
let myGroup = DispatchGroup()
//get HTTp request with Alamofire
myGroup.enter()
Alamofire.request(allCardsHTTP, method: .get).responseJSON {
response in
if response.result.isSuccess {
let jsonCards : JSON = JSON(response.result.value!)
print("success")
self.updateJSONCards(json: jsonCards)
}
else {
print("error")
}
myGroup.leave()
}
myGroup.notify(queue: .main) {
//Updte UI here
print(self.allCard[10].name) //This is Working
for card in self.allCard {
if card.cardsSet == "Basic" {
print(card.name)
}
} //For loop always showing last card ???
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - JSON Parsing
//Write the updateJSONCards method here:
func updateJSONCards(json: JSON) {
//create the cards
for (set, value) in json {
var card = Card()
card.cardsSet = set
if json[set].count != 0 {
for i in 0...json[set].count - 1 {
card.name = json[set][i]["name"].stringValue
allCard.append(card)
}
}
else {
print("The set \(set) has no cards")
}
}
}
}
您在错误的地方创建了 Card
实例。
在内部重复循环之前,您正在创建 one 实例并将 same 实例附加到 allCard
数组。如果 Card
是 class(引用语义)该实例的所有出现都包含相同的数据(最后一个循环项)。
将两行 放入 重复循环中。
//Write the updateJSONCards method here:
func updateJSONCards(json: JSON) {
//create the cards
for (set, value) in json {
if !json[set].isEmpty {
for i in 0..<json[set].count {
let card = Card()
card.cardsSet = set
card.name = json[set][i]["name"].stringValue
allCard.append(card)
}
}
else {
print("The set \(set) has no cards")
}
}
}