/WeatherApp

πŸ”† 날씨 OpenAPIλ₯Ό μ΄μš©ν•΄μ„œ Apple κΈ°λ³Έ 날씨앱 λ˜λŠ” Yahoo 날씨앱과 μœ μ‚¬ν•œ κΈ°λŠ₯셋을 κ΅¬ν˜„ν•˜κΈ°

Primary LanguageSwift

WeatherApp

날씨 OpenAPIλ₯Ό μ΄μš©ν•΄μ„œ Apple κΈ°λ³Έ 날씨앱 λ˜λŠ” Yahoo 날씨앱과 μœ μ‚¬ν•œ κΈ°λŠ₯셋을 κ΅¬ν˜„ν•˜κΈ°

 

Index


 


κΈ°λŠ₯

날씨 정보

μœ„μΉ˜ 리슀트(ν˜„μž¬ μœ„μΉ˜ + μ €μž₯된 μœ„μΉ˜)의 날씨λ₯Ό 각각 κ°€λ‘œ 슀크둀둜 μ „ν™˜ν•˜μ—¬ 쑰회 κ°€λŠ₯

μž₯μ†Œ μ €μž₯ 및 리슀트 μ‘°μž‘

μΆ”κ°€ν–ˆλ˜ μž₯μ†Œ 리슀트λ₯Ό λ³Ό 수 있고, μ‚­μ œν•  수 μžˆλ‹€

μž₯μ†Œ 검색 및 μΆ”κ°€

μ›ν•˜λŠ” μž₯μ†Œλ₯Ό κ²€μƒ‰ν•˜μ—¬ μΆ”κ°€ν•˜κ³ , κ΄€λ ¨ 날씨 정보λ₯Ό λ³Ό 수 μžˆλ‹€

μ˜¨λ„ λ‹¨μœ„ μ„€μ •

μ˜¨λ„ λ‹¨μœ„λ₯Ό 섭씨 ν˜Ήμ€ ν™”μ”¨λ‘œ λ³€ν™˜ν•˜μ—¬ λ³Ό 수 μžˆλ‹€

μ‚¬μš©μž μ„€μ • μ €μž₯

λ‹€μŒ 섀정을 μ €μž₯ν•˜μ—¬ 앱을 λ‹€μ‹œ μ‹€ν–‰μ‹œ, 기쑴의 μ„€μ •λŒ€λ‘œ μ‹€ν–‰λ˜λ„λ‘ ν•œλ‹€

μΆ”κ°€ν•œ μž₯μ†Œ μ˜¨λ„ λ‹¨μœ„ μ„€μ • λ§ˆμ§€λ§‰μœΌλ‘œ λ³Έ νŽ˜μ΄μ§€

 


섀계 및 κ΅¬ν˜„

View Controller ꡬ성

 

날씨 λͺ¨λΈκ³Ό View - MVVM

WeatherViewController - WeatherViewModel

  • ViewModel 의 view κ΄€λ ¨ type 에 Observer λ₯Ό 등둝할 수 μžˆλŠ” νƒ€μž…μ„ κ΅¬ν˜„
    • Observable protocol
    • ViewModel - CurrentWeather, HourlyWeatherItem, DailyWetherItem, DetailWeather λŒ€μƒ data 변화에 λŒ€ν•΄ observer λ₯Ό 등둝할 수 μžˆλ‹€
    • observer handler 에 κ΄€λ ¨ view λ‚˜ label text λ₯Ό λ³€κ²½ν•  수 μžˆλŠ” ν•¨μˆ˜λ₯Ό 등둝
    • view model λ³€κ²½μ‹œμ— ν•΄λ‹Ή observerκ°€ μ‹€ν–‰λ˜μ–΄ view 도 같이 그에 맞게 μ—…λ°μ΄νŠΈ λœλ‹€

 

μ—­ν•  λΆ„λ°°

view κ΄€λ ¨

class / struct μ—­ν• 
PageViewController LocationManager μ‚¬μš©ν•΄μ„œ 받은 ν˜„μž¬μœ„μΉ˜λ₯Ό ν¬ν•¨ν•˜μ—¬, μ €μž₯된 μœ„μΉ˜μ˜ 날씨λ₯Ό 보여쀄 WeatherViewController λ₯Ό 보여쀀닀
WeatherViewController Location 객체에 ν•΄λ‹Ήν•˜λŠ” μœ„μΉ˜ 정보λ₯Ό 보여쀀닀
WeatherViewModel - OpenWeatherMapService λ₯Ό μ΄μš©ν•˜μ—¬ μœ„μΉ˜μ— λ§žλŠ” 날씨 정보λ₯Ό κ°€μ Έμ˜¨λ‹€
- WeatherBuilder 객체λ₯Ό 톡해 κ°€μ Έμ˜¨ 정보λ₯Ό view 에 ν•„μš”ν•œ 데이터 νƒ€μž…λ“€λ‘œ λ§Œλ“ λ‹€
WeatherBuilder λ„€νŠΈμ›Œν‚Ήμ„ 톡해 λ°›μ•„μ˜¨ WeatherData 객체λ₯Ό view model 의 각 ν•„μš”μ— 맞게 κ°€κ³΅ν•œλ‹€
LocationListViewController - μ €μž₯된 μœ„μΉ˜λ₯Ό μ‚¬μš©μžμ—κ²Œ 보여쀀닀
- μ €μž₯된 μœ„μΉ˜λ₯Ό μ‚­μ œν•œλ‹€
- μ˜¨λ„ λ‹¨μœ„λ₯Ό λ°”κΎΌλ‹€
- SearchViewController λ₯Ό 보여쀀닀
SearchViewController - μ‚¬μš©μž 검색 λ¬Έμžμ—΄μ„ μ‚¬μš©ν•˜μ—¬ μœ„μΉ˜ μžλ™μ™„μ„±
- μ‚¬μš©μžκ°€ μ„ νƒν•œ μœ„μΉ˜ 정보(μž₯μ†Œμ΄λ¦„, μœ„λ„, 경도) λ₯Ό LocationViewController delegate 톡해 λ„˜κΈ΄λ‹€
Observable - ViewModel 의 각 데이터 νƒ€μž…μ— observer κΈ°λŠ₯을 κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ generic type
- WeatherViewModel μ—μ„œ observer 등둝이 ν•„μš”ν•œ 속성에 μ‚¬μš©λ¨

Utilities

class / struct μ—­ν• 
LocationManager CLLocationManager μ‚¬μš©ν•˜μ—¬ ν˜„μž¬ μœ„μΉ˜ κ°€μ Έμ˜€κΈ°
OpenWeatherMapService - λ„€νŠΈμ›Œν‚Ή ν†΅ν•œ 날씨 예츑 정보 κ°€μ Έμ˜€κΈ°
- WeatherData νƒ€μž…μœΌλ‘œ json decoding ν•˜κΈ°
WeatherBuilder WeatherData β†’ WeatherViewModel 이 가진 각 type으둜 데이터 가곡
WindDirection λ°”λžŒμ˜ λ°©ν–₯ κ°’(360도 λ‚΄)을 compass direction으둜 λ³€ν™˜
DateConverter 주어진 timezone 을 μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄λ‘œ 된 λ‚ μ§œλ₯Ό λ³€ν™˜ν•΄ μ£ΌλŠ” μ—­ν• 

 

Weather Model Hierarchy

 

ViewController κ°„ Data μ£Όκ³  λ°›κΈ° - Delegate μ‚¬μš©

κ΄€λ ¨ ν•™μŠ΅ν•œ λ‚΄μš©- 1 κ΄€λ ¨ ν•™μŠ΅ν•œ λ‚΄μš©- 2

view controller κ°„ 데이터λ₯Ό backward 둜 λ°›κΈ° μœ„ν•΄μ„œ, delegate protocol 을 κ΅¬ν˜„ν•˜μ—¬ μ‚¬μš©

 

ν˜„μž¬ μœ„μΉ˜ - CLLocationManager ν™œμš©

LocationMagnager class 둜 κ΅¬ν˜„

μœ„μΉ˜ κ°€μ Έμ˜€λŠ” κ³Όμ •

  1. CLLocationManager 객체 생성
  2. location λ°μ΄ν„°μ˜ 정확도 μ„€μ • : desiredAccuracy property μ„€μ •
  3. μ‚¬μš©μžμ—κ²Œ μœ„μΉ˜μ •λ³΄ μ‚¬μš© ν—ˆκ°€ λ°›κΈ° : requestWhenInUseAuthorization() method
  4. μœ„μΉ˜ μš”μ²­μ΄ κ°€λŠ₯ν•œ ν—ˆκ°€ μƒνƒœ CLAuthorizationStatus : .authorizedWhenInUse / .authorizedAlways
  5. μœ„μΉ˜ μš”μ²­: requestLocation()
    • ν•΄λ‹Ή methodλŠ” 즉각 return ν•œλ‹€
    • μœ„μΉ˜ 값을 얻은 ν›„, delegate 의 didUpdateLocation method λ₯Ό ν˜ΈμΆœν•œλ‹€
  6. Delegate method - didUpdateLocation

 

날씨 정보 λ°›μ•„μ˜€κΈ° & νŒŒμ‹±ν•˜κΈ° - OpenWeather API / URLSession / Codable

5 days / 3 hours forecast api

API JSON ꡬ쑰 (orange color : λ°°μ—΄ ꡬ쑰)

  • URLSession ν•™μŠ΅ν•œ λ‚΄μš©
  • Codable : Data β†’ WeatherData λ³€ν™˜ν•˜κΈ°
    • WeatherData λŠ” Codable protocol 을 μ€€μˆ˜
    • JSONDecoder μ‚¬μš©ν•˜μ—¬ λ³€ν™˜

 

μž₯μ†Œ 검색 & μžλ™μ™„μ„± - MKLocalSearchCompleter

λ¬Έμžμ—΄λ‘œ μœ„μΉ˜λ₯Ό μ œκ³΅ν•˜λ©΄ 그에 λ§žλŠ” μžλ™μ™„μ„±λœ comletion string list λ₯Ό μ œκ³΅ν•˜λŠ” utility 객체

  • κ΅¬ν˜„ 원리

  • results property : MKLocalSearchCompleter 의 μžλ™μ™„μ„± 처리된 데이터λ₯Ό μ–»λŠ” 속성

    • MKLocalSearchCompletion type
    • 직접 생성할 μˆ˜λŠ” μ—†λ‹€. Completer 에 μ˜ν•΄μ„œλ§Œ μƒμ„±λ˜λŠ” 객체
  • completion 될 λŒ€μƒ 지정 방법

    • μœ„μΉ˜ λ¬Έμžμ—΄, 지역, ν•„ν„° νƒ€μž… 등을 지정할 수 μžˆλ‹€.
    • λ„μ‹œλͺ… 검색 : queryFragment property 에 μ‚¬μš©μžκ°€ μž…λ ₯ν•˜λŠ” λ¬Έμžμ—΄ μ„€μ •
    • ν•„ν„° νƒ€μž… : locationAndQueries / locationsOnly
  • delgate : search completion data λ₯Ό κ°€μ Έμ˜€κΈ° μœ„ν•œ λ©”μ†Œλ“œκ°€ μ •μ˜λ¨

    • MKLocalSearchCompleterDelegate
    • completerDidUpdateResults() λ©”μ†Œλ“œ : completer κ°€ 검색 μ™„μ„± 배열을 μ—…λ°μ΄νŠΈ ν•œ λ’€ ν˜ΈμΆœν•˜λŠ” λ©”μ†Œλ“œ.
    • 이 λ©”μ†Œλ“œ μ•ˆμ— search κ²°κ³Ό table view λ₯Ό reload ν•˜λ„λ‘ κ΅¬ν˜„ν•¨

 

μ‚¬μš©μž μ„€μ • μ €μž₯ - UserDefaults

κ΄€λ ¨ ν•™μŠ΅ν•œ λ‚΄μš©

μ‚¬μš©μž μ„€μ • ν•­λͺ©

  • λ§ˆμ§€λ§‰μœΌλ‘œ λ³Έ λ‚ μ”¨μ˜ μœ„μΉ˜
  • μ‚¬μš©μžκ°€ μ €μž₯ν•œ μœ„μΉ˜ 리슀트
  • μ˜¨λ„ λ‹¨μœ„ 선택 정보

UserDefaults 에 μ‚¬μš©λ  key κ΄€λ¦¬ν•˜λŠ” struct DataKeys

 

API 데이터 기반 μ‹œκ°„ κ΅¬ν•˜κΈ°

API μ—μ„œ λ°›μ•„μ˜¨ date & time (UTC ν‘œμ€€) β†’ 각 λ‚˜λΌλ³„ μ‹œκ°„μœΌλ‘œ λ³€ν™˜ν•˜κΈ°

  1. list.dt_text string (utc λ‹¨μœ„ μ‹œκ°„) β†’ Date 객체둜 λ³€ν™˜
  2. city.timezone : ν•΄λ‹Ή λ„μ‹œμ˜ μ‹œκ°„μ„ UTCλ‘œλΆ€ν„° λ³€ν™˜ν•˜κΈ° μœ„ν•œ 차이값. λ‹¨μœ„λŠ” 초
  3. 각 λ„μ‹œμ˜ μ‹œκ°„ = list.dt_txt λ₯Ό date둜 λ³€ν™˜ν•œ 객체 + city.timezone
  4. 차이값 더해주기 : Date - addingTimeInterval() method μ‚¬μš©

 

μ˜¨λ„ λ‹¨μœ„ μ„€μ •λŒ€λ‘œ 정보 보여주기 - Singleton ν™œμš©

  • Singleton 으둜 κ΅¬ν˜„ν•œ 이유

    • view controller 뿐만 μ•„λ‹ˆλΌ 날씨 κ΄€λ ¨λœ 거의 λͺ¨λ“  data model μ—μ„œ μ˜¨λ„μ™€ κ΄€λ ¨λœ 뢀뢄이 많음
    • Singleton ν†΅ν•΄μ„œ ν•˜λ‚˜μ˜ μΈμŠ€ν„΄μŠ€λ‘œ μ‚¬μš©μžκ°€ μ„€μ •ν•œ μ˜¨λ„ λ‹¨μœ„λ₯Ό μ΄μš©ν•˜λŠ”κ²Œ μ μ ˆν•˜λ‹€κ³  νŒλ‹¨
  • TemperatureUnit 의 shared property 둜 λ‹¨μœ„ μ ‘κ·Ό κ°€λŠ₯

 


Trouble Shooting

Page View Controller μ—μ„œ κ³„μ†ν•΄μ„œ WeatherViewController λ₯Ό μƒμ„±ν•˜λŠ” 문제

  • λ¬Έμ œμƒν™©
    • PageViewController μ—μ„œ swipe 에 λ”°λ₯Έ 이전/이후 νŽ˜μ΄μ§€ μš”μ²­μ‹œλ§ˆλ‹€ μƒˆλ‘­κ²Œ view controller instance (WeatherViewController) λ₯Ό 생성
    • λ©”λͺ¨λ¦¬ λΆ€ν•˜λ‘œ 인해 κ°‘μžκΈ° κΊΌμ§€λŠ” ν˜„μƒ
  • ν•΄κ²° 방법 : View Controller Caching - NSCache
    • NSCache

      • 캐싱을 κ΅¬ν˜„ν•œ 클래슀둜 mutable dictionary type 처럼 μ‚¬μš©ν•  수 μžˆλ‹€ - key λ₯Ό 톡해 μΊμ‹±ν•œ μΈμŠ€ν„΄μŠ€ μ ‘κ·Ό κ°€λŠ₯
      • λ””λ°”μ΄μŠ€κ°€ λ©”λͺ¨λ¦¬ 뢀쑱을 κ²ͺ을 λ•Œ 이 μΊμ‹œμ— μžˆλŠ” μΈμŠ€ν„΄μŠ€μ˜ λ©”λͺ¨λ¦¬ 곡간을 μžλ™μœΌλ‘œ νšŒμˆ˜ν•¨
      var cachedWeatherViewControllers = NSCache<NSNumber, WeatherViewController>()
    • view controllerλ₯Ό ν•œλ²ˆ μƒμ„±ν•œ λ’€, caching ν•˜μ—¬ μ΄ν›„μ˜ view controller 의 μš”μ²­μ΄ μžˆμ„ λ•Œ, 이미 μΈμŠ€ν„΄μŠ€κ°€ μžˆλ‹€λ©΄ 이λ₯Ό λ°˜ν™˜ν•˜λŠ” κΈ°λŠ₯을 κ΅¬ν˜„

    • PageViewController μ—μ„œ μΊμ‹±ν•œ WeatherViewController μΈμŠ€ν„΄μŠ€ 관리

      • Key: page index (NSNumber type)
      • Value: WeatherViewController instance

 

λ§ˆμ§€λ§‰μœΌλ‘œ λ³Έ μœ„μΉ˜λ₯Ό λ¦¬μŠ€νŠΈμ—μ„œ μ‚­μ œν•  경우

  • λ¬Έμ œμƒν™©
    • λ§ˆμ§€λ§‰μœΌλ‘œ 날씨 정보λ₯Ό λ³Έ μœ„μΉ˜λ₯Ό λ¦¬μŠ€νŠΈμ—μ„œ μ‚­μ œν•  경우, λ‹€μ‹œ page view 둜 λŒμ•„κ°”μ„ λ•Œ page index κ°€ 뢈일치 ν•˜λŠ” ν˜„μƒ
  • ν•΄κ²° 방법
    • μ‚­μ œμ‹œ μœ„μΉ˜μ •λ³΄μ˜ index κ°€ λ§ˆμ§€λ§‰μΌ 경우, λ§ˆμ§€λ§‰μœΌλ‘œ λ³Έ list index λ₯Ό 0으둜 μ„€μ •ν•œλ‹€.

 

API μ—μ„œ λ°›μ•„μ˜¨ 데이터 - μ‹œκ°„λ³„ O 일별 X

  • λ¬Έμ œμƒν™©
    • open weather map 의 5일 / 3μ‹œκ°„ api μ—λŠ” 3μ‹œκ°„ λ§ˆλ‹€μ˜ 날씨 예츑 μ •λ³΄λŠ” μžˆμ§€λ§Œ, 일별 예츑 μ •λ³΄λŠ” μ—†λ‹€.
    • λ”°λΌμ„œ, 3μ‹œκ°„ λ§ˆλ‹€μ˜ 데이터λ₯Ό μΌλ³„λ‘œ λΆ„λ₯˜ν•˜κ³  이λ₯Ό λ‹€μ‹œ κ°€κ³΅ν•˜λŠ” μ ˆμ°¨κ°€ ν•„μš”
  • 해결방법
    • WeatherData β†’ WeatherViewModel 이 가진 각 model λ³„λ‘œ κ°€κ³΅ν•˜λŠ” 역할을 λ‹΄λ‹Ήν•˜λŠ” 클래슀 κ΅¬ν˜„
    • WeatherBuilder
    • 3μ‹œκ°„ 별 데이터λ₯Ό μΌμžλ³„λ‘œ λͺ¨μ•„μ„œ, μΌμžλ³„ μ΅œλŒ€/μ΅œμ†Œ μ˜¨λ„λ₯Ό 계산 β†’ HourlyWeatherItem 으둜 λ§Œλ“€κΈ°

 


κ΄€λ ¨ ν•™μŠ΅ λ‚΄μš©

URL Loading System

ν‘œμ€€ 인터넷 ν”„λ‘œν† μ½œμ„ μ‚¬μš©ν•˜μ—¬ μ„œλ²„μ™€ url 둜 μ†Œν†΅ν•˜λŠ” 방식

URL둜 확인할 수 μžˆλŠ” λ¦¬μ†ŒμŠ€μ— μ ‘κ·Όν•˜λŠ” 방식을 URL Loading System 이라 ν•œλ‹€.

resource loading 은 asynchronously (비동기) 둜 μˆ˜ν–‰λ˜λ―€λ‘œ, μœ μ €μ˜ μ΄λ²€νŠΈμ— 응닡할 수 있고 λ“€μ–΄μ˜€λŠ” λ°μ΄ν„°λ‚˜ μ—λŸ¬λ₯Ό μ²˜λ¦¬ν•  수 μžˆλ‹€.

URLSession

Url 둜 request λ₯Ό λ³΄λ‚΄κ±°λ‚˜ λ°›λŠ” 일을 λ‹΄λ‹Ήν•˜λŠ” 객체

  • μ„€μ • : URLSessionConfiguration
    • default
    • ephemeral
    • background
  • URLSession instance λŠ” URLSessionTask μΈμŠ€ν„΄μŠ€λ₯Ό ν•œκ°œ 이상 μƒμ„±ν•˜μ—¬ μ‚¬μš©ν•œλ‹€.
    • GET request 톡해 데이터λ₯Ό λ°›μ•„μ˜€λŠ” 일 : URLSessionDataTask
    • POST / PUT request 톡해 νŒŒμΌμ„ μ—…λ‘œλ“œ ν•˜λŠ” 일 : URLSessionUploadTask
    • 원격 μ„œλ²„μ—μ„œ νŒŒμΌμ„ λ‹€μš΄λ‘œλ“œ ν•΄μ˜€λŠ” 일 : URLSessionDownloadTask
  • Task μƒνƒœ
    • suspend
    • resume
    • cancel
  • URLSession 이 데이터λ₯Ό λ°˜ν™˜ν•˜λŠ” 두가지 방법 (λΉ„λ™κΈ°μ μœΌλ‘œ μˆ˜ν–‰λ˜λ―€λ‘œ, 끝남을 μ•Œλ¦¬λŠ” 방법)
    1. completion handler - task κ°€ 끝날 λ•Œ 싀행됨
    2. delegate 의 method 호좜

URLComponents

URL 을 κ΅¬μ„±ν•˜λŠ” μš”μ†Œλ“€μ„ ꡬ쑰체둜 λ‚˜νƒ€λƒ„

  • queryItem property : URLQueryItem (name -value 짝으둜 κ΅¬μ„±λ˜μ–΄ URL 의 query 뢀뢄을 λ‹΄λ‹Ή)
  • url property : κ΅¬μ„±μš”μ†Œλ“€λ‘œλΆ€ν„° μƒμ„±λœ URL

λ„€νŠΈμ›Œν¬

데이터λ₯Ό URL λ‘œλΆ€ν„° κ°€μ Έμ˜€λ €λ©΄

  • μ–΄λ–€ 데이터λ₯Ό μ£Όμ„Έμš” : request
  • 응닡 : reponse

URLSession ν™œμš©ν•œ data GET

func dataTask(with url: URL, 
completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
  • shared : singleton URLSession instance - κ°„λ‹¨ν•œ request μš©λ„
let task = URLSession.shared.dataTask(with: url) {
	// completion handler
}
  • Data : bytes or nil(error)

  • reponse: reponse 의 κ΅¬ν˜„μ²΄. HTTPURLReponse 둜 νƒ€μž… μΊμŠ€νŒ… κ°€λŠ₯

  • error: error λ°œμƒμ‹œ κ°’ 있음. Nil 이면 성곡

  • λ„€νŠΈμ›Œν¬ μ’…λ£Œμ‹œ resume()

GCD (Grand Central Dispatch)

비동기 μˆ˜ν–‰μ„ 원할 λ•Œ, main queue(main flow) 말고 λ‹€λ₯Έ μˆ˜ν–‰ queue 둜 μž‘μ—…μ„ 보내고 싢을 λ•Œ μ‚¬μš©

main queue : system 이 μ œκ³΅ν•˜λŠ” queue둜 λͺ¨λ“  UI code κ°€ μˆ˜ν–‰λ˜μ–΄μ•Ό ν•˜λŠ” κ³³

var items: [Item]?

DispatchQueue.main.async {
    items = findItems(matching: "News")
}
// async ν΄λ‘œμ Έκ°€ μ‹€ν–‰ μ™„λ£Œ 될 λ•Œ, item 에 값이 ν• λ‹Ή λœλ‹€.
// λ‹¨μˆœνžˆ μ € μ„ μ–Έλ¬Έ μ§€λ‚˜κ°”λ‹€κ³  μ‹€ν–‰ μ™„λ£Œλœκ²Œ μ•„λ‹˜

network request 같은 무거운 μž‘μ—…μ„ ν•  λ•ŒλŠ” background queue μ—μ„œ μ‹€ν–‰λ˜λŠ”κ²Œ μ•±μ˜ main μ—μ„œ μ‹€ν–‰λ˜λŠ”κ²ƒλ³΄λ‹€ ꢌμž₯됨. - UI λŠ” λ‹€λ₯Έ 일을 ν•˜λŠ” λ™μ•ˆ 무거운 μž‘μ—…λ“€μ„ λ’€μ—μ„œ ν•  수 μžˆμœΌλ―€λ‘œ

View Controller μ–‘λ°©ν–₯ 데이터 전달

  • view controller present 관계
    • presenting view controller : λ‚˜(view controller) λ₯Ό λ„μ›Œμ€€, 보여쀀 view controller
    • presented view controller : λ‚΄κ°€ (view controller) present ν•˜λŠ”, λ„μš΄, 보여쀀 view controller

  • view controller κ°„ 데이터 전달
    • 보여쀄 view controller μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ—¬ present() method
    • λ‹€μ‹œ ν˜„μž¬μ˜ view controller λ₯Ό 보여쀀 μ΄μ „μ˜ view controller 둜 데이터λ₯Ό 보내렀면? delegate 톡해 전달~전달
      • μ‰½κ²Œ λ§ν•΄μ„œ μƒˆλ‘œμš΄ view controller λ₯Ό instantiate ν•΄μ„œ present ν•˜κΈ° 전에 λ‚΄κ°€ 널 λ³΄λƒˆλ‹€λŠ” 연결을 delegate λ₯Ό 톡해 ν•˜λŠ” 것이닀
      • κ·Έ μ—°κ²° 방식은 protocol 을 ν†΅ν•΄μ„œ ν•΄λ‹Ή protocol을 κ΅¬ν˜„ν•œ νƒ€μž…μ΄λΌλ©΄ λˆ„κ΅¬λ‚˜ delegate 둜 μ†Œν†΅ν•  수 μžˆλ‹€λŠ” λœ»μ΄λ‹€.

UserDefaults

μ•±μ˜ data λ₯Ό λ°±κ·ΈλΌμš΄λ“œ μƒνƒœ ν˜Ήμ€ μ’…λ£Œμ‹œμ—λ„ 없어지지 μ•Šκ³  persistent(영ꡬ) 보쑴할 수 μžˆλ„λ‘ ν•΄μ£ΌλŠ” user default database

  • key-value ν˜•νƒœλ‘œ μ €μž₯λœλ‹€. Key λŠ” String 만 κ°€λŠ₯
  • μ €μž₯ κ°€λŠ₯ν•œ Value ν˜•νƒœ : NSData, NSString, NSNumber, NSArray, NSDictionary
  • νŠΉμ„±
    • UserDefaults 톡해 κ°€μ Έμ˜¨ λ°μ΄ν„°λŠ” immutable
    • plist extension 으둜 μ €μž₯됨
    • app launch 될 λ•Œ, memory 에 μ˜¬λΌμ˜¨λ‹€.
  • UserDefaults 변경에 μ•Œλ¦Όμ„ λ°›κ³  μ‹Άλ‹€λ©΄
    • didChangeNotification 에 observer λ₯Ό λ“±λ‘ν•˜λ©΄ λœλ‹€.