2017-10-15 73 views
-1

我有一個「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) 
+0

什麼是錯誤日誌準確地說? – Tushar

+0

@Tushar致命錯誤:索引超出範圍 – eonr

+0

Swift約定是對變量使用lowerCamelCase名稱,而不是snake_case。另外,你應該在'var'變量和'append'調用上'let'常量和數組文字。 – Alexander

回答

0

self.first_arrayfirst_array指的是不同的東西打印出來。

+0

我加了整個代碼。你可以請檢查一下嗎? – eonr

+0

你應該看看使用Swift 4中的Codable來減輕你編寫所有這些瘋狂的json代碼。 – Alexander

+0

我現在在使用Swift 3。 – eonr

0

如果上面的代碼是正確的,那麼問題是self.teams與本地變量var teams = [[Team]]()不一樣。與self.first_array和本地變量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

+0

我添加了整個代碼。你可以請檢查一下嗎? – eonr

-1

我在下面的代碼中調用問題的調用函數。

self.teams = [[]] 

我改變它並且它現在工作。

self.teams = [[],[]] 
相關問題