二维数组在swift中给出索引超出范围的错误

时间:2017-10-15 17:59:34

标签: ios arrays swift

我有一个二维数组" Team"对象并拥有一个" Team"当我尝试从2d数组打印附加数组时,它会出错。在下面的代码中,打印出错。

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]

错误:

  

致命错误:索引超出范围

我可以用

打印出来
print(self.teams.popLast()?[0].name)

3 个答案:

答案 0 :(得分:0)

self.first_arrayfirst_array指的是不同的东西。

答案 1 :(得分:0)

如果上面的代码是正确的,则问题是self.teams与局部变量var teams = [[Team]]()不同。与self.first_array和本地var first_array相同。希望你明白这一点。

要解决此问题,请执行以下任一操作:

而不是写作 var teams = [[Team]]() var first_array = [Team]()

self.teams = [[Team]]() self.first_array = [Team]() 或者,

而不是self.teams.append(self.first_array),请写teams.append(first_array)然后self.teams = teams

答案 2 :(得分:-1)

我在下面的代码中调用了有问题的调用函数。

 self.teams = [[]]

我把它改成了它现在正在工作。

self.teams = [[],[]]