我有一個「Team」對象的二維數組,並且當我嘗試從2d數組打印附加數組時,它有一個「Team」數組,它會給出錯誤。在下面的代碼打印出錯。二維數組給出了索引超出範圍內的錯誤
self.first_array = []
for i in 0..<json["LeagueStage"][0]["LeagueTable"].count {
guard let name = json["LeagueStage"][0]["LeagueTable"][i]["name"].string,
let id = json["LeagueStage"][0]["LeagueTable"][i]["id"].int,
let teamId = json["LeagueStage"][0]["LeagueTable"][i]["TeamID"].int,
let points = json["LeagueStage"][0]["LeagueTable"][i]["points"].string,
let played = json["LeagueStage"][0]["LeagueTable"][i]["played"].string,
let wins = json["LeagueStage"][0]["LeagueTable"][i]["wins"].string,
let draws = json["LeagueStage"][0]["LeagueTable"][i]["draws"].string,
let defeits = json["LeagueStage"][0]["LeagueTable"][i]["defeits"].string,
let goalsfor = json["LeagueStage"][0]["LeagueTable"][i]["goalsfor"].string,
let goalsagainst = json["LeagueStage"][0]["LeagueTable"][i]["goalsagainst"].string,
let average = json["LeagueStage"][0]["LeagueTable"][i]["average"].int,
let rank = json["LeagueStage"][0]["LeagueTable"][i]["rank"].int,
let hasTeamLogo = json["LeagueStage"][0]["LeagueTable"][i]["HasTeamLogo"].bool,
let imageUrl = json["LeagueStage"][0]["LeagueTable"][i]["TeamLogo"].string
else {
let dataFetch: [AnyHashable : Any] =
[
NSLocalizedDescriptionKey : NSLocalizedString("Veri Yok", value: "Hata", comment: "") ,
NSLocalizedFailureReasonErrorKey : NSLocalizedString("Veri Yok", value: "Hata", comment: "") ]
let err = NSError(domain: "ShiploopHttpResponseErrorDomain", code: 401, userInfo: dataFetch)
self.delegate?.errorUpdating(err as NSError)
return
}
let temp_team = Team(id: id,
teamId: teamId,
name: name,
points: Int(points)!,
played: Int(played)!,
wins: Int(wins)!,
draws: Int(draws)!,
defeits: Int(defeits)!,
goalsfor: Int(goalsfor)!,
goalsagainst: Int(goalsagainst)!,
average: average,
rank: rank,
HasTeamLogo: hasTeamLogo,
imageurl: imageUrl
)
self.first_array.append(temp_team)
}
}
self.teams.append(first_array)
print(teams[0][0].name)
初始化部分
// MARK: - Variables
var teams = [[Team]]()
var first_array = [Team]()
我敢肯定有與first_array沒有問題,因爲當我做類似下面,它的工作原理。
self.teams = [self.first_array, self.first_array]
錯誤:
fatal error: Index out of range
,我可以用
print(self.teams.popLast()?[0].name)
什麼是錯誤日誌準確地說? – Tushar
@Tushar致命錯誤:索引超出範圍 – eonr
Swift約定是對變量使用lowerCamelCase名稱,而不是snake_case。另外,你應該在'var'變量和'append'調用上'let'常量和數組文字。 – Alexander