Skip to content

4주차_issue #6

@leewoojye

Description

@leewoojye

⭐️ 4주차 과제 핵심 포인트 ⭐️

  • 기본&심화과제 모두 구현했습니다! 날씨상세뷰에서 시간대별 날씨는 3시간 단위로 날씨를 제공하는 API를 사용했습니다. (Hourly forecast API가 유료인 관계로...)
  • 서버에서 원하는 날씨정보를 API 호출을 통해 가져오기
  • 서버에서 받아온 JSON 객체를 구조체로 변환하고, 해당 구조체에서 사용할 정보만을 또 추출하기
  • JSON에서 받아온 데이터들을 구조체.swift 파일을 통해 원하는 데이터 포맷으로 변환하기
  • 홈화면 테이블뷰 셀 겹칩 현상 해결
  • 날씨상세뷰의 10일간 일기예보에는 API가 반영되지 않았습니다.

🔨 구현 방안 개요 🔨

  • JSON -> 구조체 변환은 quicktype.io를 통해 변환했습니다.
  • JSON 데이터를 받아오는 것과 별개로, ItemListData, ImageCollectionData 스위프트 파일에서 앱에 필요한 형태의 정보로 가공하는 과정을 거쳤습니다.

📍 핵심 코드 설명 📍

SecondViewController.swift

    private func fetchWeatherInfo() async {
        let city = self.Location
        imageCollectionList = [] // append를 쓰기 때문에 배열 초기화해줘야함
        
        for i in 0...11 {
            do {
                guard let currentWeather = try await GetTimeline.shared.GetTimelineData(cityname: city) else {return}
                let weatherInfo = ImageCollectionData(time: currentWeather.list[i].dtTxt, weather: currentWeather.list[i].weather[0].main.rawValue, temperature: currentWeather.list[i].main.temp)
                
                imageCollectionList.append(weatherInfo)
            } catch {
                print(error)
            }
        }
        
        do {
            guard let currentWeather = try await GetTimeline.shared.GetTimelineData(cityname: city) else {return}
            guard let time = extractHour(from: currentWeather.list[0].dtTxt) else {return}
            self.descriptionView.text = "\(String(describing: time))에 \(self.Weather) 상태가 예상됩니다."
        } catch {
            print(error)
        }
        collectionview.reloadData()
    }

do-catch 문을 두 번 이용하여 한 번은 시간대별 날씨 데이터를 배열에 넣어주고, 다른 한 번은 현재의 날씨를 라벨로 표현해주었습니다. 홈화면의 테이블뷰와 달리 시간대별 날씨에 사용된 콜렉션뷰에는 초반에 배열 초기화를 진행하여 다른 장소의 날씨데이터가 누적되는 현상을 방지했습니다.
또 시간대별 날씨 아이콘의 경우 3-hourly API에서 제공된 날씨 enum, 즉 Clear, Clouds, Snow, Rain 날씨를 네 종류로 간략화 한 뒤 기상 아이콘과 매칭해 주었습니다.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions