API 설정하기

- API 브랜치로 check out을 하고 main의 내용을 pull 받는다.


- 목적에 맞는 JSON 데이터를 찾고 데이터를 확인해본다.

- 클래스에 방금 찾은 URL에 API 키를 조합하여 저장한다.

- getData()함수를 선언하고 URL을 통해 JSON 데이터를 받아와 디코딩까지 끝낸 모습
import Foundation
-----------------------------------
// MARK: - TeamElement
struct TeamElement: Codable {
let teamKey, teamName: String
let teamBadge: String
let players: [Player]
let coaches: [Coach]
enum CodingKeys: String, CodingKey {
case teamKey = "team_key"
case teamName = "team_name"
case teamBadge = "team_badge"
case players, coaches
}
}
-----------------------------------
// MARK: - Coach
struct Coach: Codable {
let coachName, coachCountry, coachAge: String
enum CodingKeys: String, CodingKey {
case coachName = "coach_name"
case coachCountry = "coach_country"
case coachAge = "coach_age"
}
}
-----------------------------------
// MARK: - Player
struct Player: Codable {
let playerKey: Int
let playerID: String
let playerImage: String
let playerName, playerNumber, playerCountry: String
let playerType: PlayerType
let playerAge, playerMatchPlayed, playerGoals, playerYellowCards: String
let playerRedCards: String
let playerInjured: PlayerInjured
let playerSubstituteOut, playerSubstitutesOnBench, playerAssists, playerIsCaptain: String
let playerShotsTotal, playerGoalsConceded, playerFoulsCommitted, playerTackles: String
let playerBlocks, playerCrossesTotal, playerInterceptions, playerClearances: String
let playerDispossesed, playerSaves, playerInsideBoxSaves, playerDuelsTotal: String
let playerDuelsWon, playerDribbleAttempts, playerDribbleSucc, playerPenComm: String
let playerPenWon, playerPenScored, playerPenMissed, playerPasses: String
let playerPassesAccuracy, playerKeyPasses, playerWoordworks, playerRating: String
enum CodingKeys: String, CodingKey {
case playerKey = "player_key"
case playerID = "player_id"
case playerImage = "player_image"
case playerName = "player_name"
case playerNumber = "player_number"
case playerCountry = "player_country"
case playerType = "player_type"
case playerAge = "player_age"
case playerMatchPlayed = "player_match_played"
case playerGoals = "player_goals"
case playerYellowCards = "player_yellow_cards"
case playerRedCards = "player_red_cards"
case playerInjured = "player_injured"
case playerSubstituteOut = "player_substitute_out"
case playerSubstitutesOnBench = "player_substitutes_on_bench"
case playerAssists = "player_assists"
case playerIsCaptain = "player_is_captain"
case playerShotsTotal = "player_shots_total"
case playerGoalsConceded = "player_goals_conceded"
case playerFoulsCommitted = "player_fouls_committed"
case playerTackles = "player_tackles"
case playerBlocks = "player_blocks"
case playerCrossesTotal = "player_crosses_total"
case playerInterceptions = "player_interceptions"
case playerClearances = "player_clearances"
case playerDispossesed = "player_dispossesed"
case playerSaves = "player_saves"
case playerInsideBoxSaves = "player_inside_box_saves"
case playerDuelsTotal = "player_duels_total"
case playerDuelsWon = "player_duels_won"
case playerDribbleAttempts = "player_dribble_attempts"
case playerDribbleSucc = "player_dribble_succ"
case playerPenComm = "player_pen_comm"
case playerPenWon = "player_pen_won"
case playerPenScored = "player_pen_scored"
case playerPenMissed = "player_pen_missed"
case playerPasses = "player_passes"
case playerPassesAccuracy = "player_passes_accuracy"
case playerKeyPasses = "player_key_passes"
case playerWoordworks = "player_woordworks"
case playerRating = "player_rating"
}
}
enum PlayerInjured: String, Codable {
case no = "No"
case yes = "Yes"
}
enum PlayerType: String, Codable {
case defenders = "Defenders"
case forwards = "Forwards"
case goalkeepers = "Goalkeepers"
case midfielders = "Midfielders"
}
-----------------------------------
typealias Welcome = [WelcomeElement]
- JSON 데이터를 파싱하기 위한 구조체 만들기
enum CodingKeys: String, CodingKey 를 따로 만든 이유
- JSON 데이터의 키값은 스네이크형식으로 작성되어 있지만 애플에서는 스네이크 형식의 코드를 선호하지 않아 이 키 값을 카멜형식으로 바꿔주기 위한 프로토콜이다

마무리 해야 할 점
- 이미지를 URL로 불러오기
- playerType을 이용하여 포지션을 출력하기
- 팀에 대한 정보 끝내기
- 테이블을 터치했을 경우에 튕기는 현상 체크하기