IFP 겨울방학 프로젝트 | API 작업(2)

유재우·2022년 3월 8일
0

API로 포지션 출력하기


plCell.playerPosLbl.text = footballData?[0].players[indexPath.row].playerType
  • 위 코드처럼 작성을 하면 오류가 떠서 먼저 타입을 확인을 했다.

  • 타입을 확인 했을 때에 옵셔널으로 어떠한 값들이 나왔다.

  • 코드 뒤에 .을 붙이고 Value값이 있는 것을 한번씩 시험해봤는데 rawValue값을 붙인것이 내가 원하는 값이 나왔다.

  • 원하는 모든 값이 잘 출력되는 모습

현재 이미지를 URL로 불러와서 출력하는 코드는 잘 구현이 되었는데 이런 식으로 직접 url을 쳐야만 나오는 것을 고쳐야한다.

let playerImgUrl = URL(string: "\(footballData?[0].players[indexPath.row].playerImage)")

이런식으로 해봐도 안되고

 if let img = footballData?[0].players[indexPath.row].playerImage {
            print(type(of: img))
            let playerImgUrl: URL! = URL(string: img)
            print(type(of: playerImgUrl))
            do{
                let imageData = try! Data(contentsOf: playerImgUrl!)
                plCell.playerImg.image = UIImage(data: imageData)
                DispatchQueue.main.async {
                    self.playerTable.reloadData()
                } // 메인 스레드에서 playerTable을 reloadData한다.
            } catch {
                print("error")
            }
        }

옵셔널 바인딩을 통해 상수에 string값을 넣고 그 상수를 이용하여 코드를 짜도 에러가 뜬다.

profile
끝없이 탐구하는 iOS 개발자 유재우입니다!

0개의 댓글

관련 채용 정보