λ μ¨ OpenAPIλ₯Ό μ΄μ©ν΄μ Apple κΈ°λ³Έ λ μ¨μ± λλ Yahoo λ μ¨μ±κ³Ό μ μ¬ν κΈ°λ₯μ μ ꡬννκΈ°
- λ μ¨ μ 보
- μ₯μ μ μ₯ 리μ€νΈ
- μ₯μ κ²μ λ° μΆκ°
- μ¨λ λ¨μ μ€μ
- μ¬μ©μ μ€μ μ μ₯
μμΉ λ¦¬μ€νΈ(νμ¬ μμΉ + μ μ₯λ μμΉ)μ λ μ¨λ₯Ό κ°κ° κ°λ‘ μ€ν¬λ‘€λ‘ μ ννμ¬ μ‘°ν κ°λ₯
μΆκ°νλ μ₯μ 리μ€νΈλ₯Ό λ³Ό μ μκ³ , μμ ν μ μλ€
μνλ μ₯μλ₯Ό κ²μνμ¬ μΆκ°νκ³ , κ΄λ ¨ λ μ¨ μ 보λ₯Ό λ³Ό μ μλ€
μ¨λ λ¨μλ₯Ό μμ¨ νΉμ νμ¨λ‘ λ³ννμ¬ λ³Ό μ μλ€
λ€μ μ€μ μ μ μ₯νμ¬ μ±μ λ€μ μ€νμ, κΈ°μ‘΄μ μ€μ λλ‘ μ€νλλλ‘ νλ€
μΆκ°ν μ₯μ | μ¨λ λ¨μ μ€μ | λ§μ§λ§μΌλ‘ λ³Έ νμ΄μ§ |
---|---|---|
- ViewModel μ view κ΄λ ¨ type μ Observer λ₯Ό λ±λ‘ν μ μλ νμ
μ ꡬν
Observable
protocol- ViewModel - CurrentWeather, HourlyWeatherItem, DailyWetherItem, DetailWeather λμ data λ³νμ λν΄ observer λ₯Ό λ±λ‘ν μ μλ€
- observer handler μ κ΄λ ¨ view λ label text λ₯Ό λ³κ²½ν μ μλ ν¨μλ₯Ό λ±λ‘
- view model λ³κ²½μμ ν΄λΉ observerκ° μ€νλμ΄ 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 λ±λ‘μ΄ νμν μμ±μ μ¬μ©λ¨ |
class / struct | μν |
---|---|
LocationManager |
CLLocationManager μ¬μ©νμ¬ νμ¬ μμΉ κ°μ Έμ€κΈ° |
OpenWeatherMapService |
- λ€νΈμνΉ ν΅ν λ μ¨ μμΈ‘ μ 보 κ°μ Έμ€κΈ° - WeatherData νμ
μΌλ‘ json decoding νκΈ° |
WeatherBuilder |
WeatherData β WeatherViewModel μ΄ κ°μ§ κ° typeμΌλ‘ λ°μ΄ν° κ°κ³΅ |
WindDirection |
λ°λμ λ°©ν₯ κ°(360λ λ΄)μ compass directionμΌλ‘ λ³ν |
DateConverter |
μ£Όμ΄μ§ timezone μ μ¬μ©νμ¬ λ¬Έμμ΄λ‘ λ λ μ§λ₯Ό λ³νν΄ μ£Όλ μν |
κ΄λ ¨ νμ΅ν λ΄μ©- 1 κ΄λ ¨ νμ΅ν λ΄μ©- 2
view controller κ° λ°μ΄ν°λ₯Ό backward λ‘ λ°κΈ° μν΄μ, delegate protocol μ ꡬννμ¬ μ¬μ©
LocationMagnager
class λ‘ κ΅¬ν
CLLocationManager
κ°μ²΄ μμ±- location λ°μ΄ν°μ μ νλ μ€μ :
desiredAccuracy
property μ€μ - μ¬μ©μμκ² μμΉμ 보 μ¬μ© νκ° λ°κΈ° :
requestWhenInUseAuthorization()
method - μμΉ μμ²μ΄ κ°λ₯ν νκ° μν
CLAuthorizationStatus
:.authorizedWhenInUse
/.authorizedAlways
- μμΉ μμ²:
requestLocation()
- ν΄λΉ methodλ μ¦κ° return νλ€
- μμΉ κ°μ μ»μ ν, delegate μ
didUpdateLocation
method λ₯Ό νΈμΆνλ€
- Delegate method -
didUpdateLocation
API JSON ꡬ쑰 (orange color : λ°°μ΄ κ΅¬μ‘°)
- URLSession νμ΅ν λ΄μ©
- Codable :
Data
βWeatherData
λ³ννκΈ°WeatherData
λCodable
protocol μ μ€μJSONDecoder
μ¬μ©νμ¬ λ³ν
λ¬Έμμ΄λ‘ μμΉλ₯Ό μ 곡νλ©΄ κ·Έμ λ§λ μλμμ±λ 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 μ μ¬μ©λ key κ΄λ¦¬νλ struct DataKeys
API μμ λ°μμ¨ date & time (UTC νμ€) β κ° λλΌλ³ μκ°μΌλ‘ λ³ννκΈ°
list.dt_text
string (utc λ¨μ μκ°) βDate
κ°μ²΄λ‘ λ³νcity.timezone
: ν΄λΉ λμμ μκ°μ UTCλ‘λΆν° λ³ννκΈ° μν μ°¨μ΄κ°. λ¨μλ μ΄- κ° λμμ μκ° =
list.dt_txt
λ₯Ό dateλ‘ λ³νν κ°μ²΄ +city.timezone
- μ°¨μ΄κ° λν΄μ£ΌκΈ° :
Date
-addingTimeInterval()
method μ¬μ©
-
Singleton μΌλ‘ ꡬνν μ΄μ
- view controller λΏλ§ μλλΌ λ μ¨ κ΄λ ¨λ κ±°μ λͺ¨λ data model μμ μ¨λμ κ΄λ ¨λ λΆλΆμ΄ λ§μ
- Singleton ν΅ν΄μ νλμ μΈμ€ν΄μ€λ‘ μ¬μ©μκ° μ€μ ν μ¨λ λ¨μλ₯Ό μ΄μ©νλκ² μ μ νλ€κ³ νλ¨
-
TemperatureUnit
μshared
property λ‘ λ¨μ μ κ·Ό κ°λ₯
- λ¬Έμ μν©
- 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
- Key: page index (
-
- λ¬Έμ μν©
- λ§μ§λ§μΌλ‘ λ μ¨ μ 보λ₯Ό λ³Έ μμΉλ₯Ό 리μ€νΈμμ μμ ν κ²½μ°, λ€μ page view λ‘ λμκ°μ λ page index κ° λΆμΌμΉ νλ νμ
- ν΄κ²° λ°©λ²
- μμ μ μμΉμ 보μ index κ° λ§μ§λ§μΌ κ²½μ°, λ§μ§λ§μΌλ‘ λ³Έ list index λ₯Ό 0μΌλ‘ μ€μ νλ€.
- λ¬Έμ μν©
- open weather map μ 5μΌ / 3μκ° api μλ 3μκ° λ§λ€μ λ μ¨ μμΈ‘ μ 보λ μμ§λ§, μΌλ³ μμΈ‘ μ 보λ μλ€.
- λ°λΌμ, 3μκ° λ§λ€μ λ°μ΄ν°λ₯Ό μΌλ³λ‘ λΆλ₯νκ³ μ΄λ₯Ό λ€μ κ°κ³΅νλ μ μ°¨κ° νμ
- ν΄κ²°λ°©λ²
WeatherData
βWeatherViewModel
μ΄ κ°μ§ κ° model λ³λ‘ κ°κ³΅νλ μν μ λ΄λΉνλ ν΄λμ€ κ΅¬νWeatherBuilder
- 3μκ° λ³ λ°μ΄ν°λ₯Ό μΌμλ³λ‘ λͺ¨μμ, μΌμλ³ μ΅λ/μ΅μ μ¨λλ₯Ό κ³μ° β
HourlyWeatherItem
μΌλ‘ λ§λ€κΈ°
νμ€ μΈν°λ· νλ‘ν μ½μ μ¬μ©νμ¬ μλ²μ url λ‘ μν΅νλ λ°©μ
URLλ‘ νμΈν μ μλ 리μμ€μ μ κ·Όνλ λ°©μμ URL Loading System μ΄λΌ νλ€.
resource loading μ asynchronously (λΉλκΈ°) λ‘ μνλλ―λ‘, μ μ μ μ΄λ²€νΈμ μλ΅ν μ μκ³ λ€μ΄μ€λ λ°μ΄ν°λ μλ¬λ₯Ό μ²λ¦¬ν μ μλ€.
Url λ‘ request λ₯Ό 보λ΄κ±°λ λ°λ μΌμ λ΄λΉνλ κ°μ²΄
- μ€μ :
URLSessionConfiguration
- default
- ephemeral
- background
URLSession
instance λURLSessionTask
μΈμ€ν΄μ€λ₯Ό νκ° μ΄μ μμ±νμ¬ μ¬μ©νλ€.- GET request ν΅ν΄ λ°μ΄ν°λ₯Ό λ°μμ€λ μΌ :
URLSessionDataTask
- POST / PUT request ν΅ν΄ νμΌμ μ
λ‘λ νλ μΌ :
URLSessionUploadTask
- μ격 μλ²μμ νμΌμ λ€μ΄λ‘λ ν΄μ€λ μΌ :
URLSessionDownloadTask
- GET request ν΅ν΄ λ°μ΄ν°λ₯Ό λ°μμ€λ μΌ :
- Task μν
- suspend
- resume
- cancel
- URLSession μ΄ λ°μ΄ν°λ₯Ό λ°ννλ λκ°μ§ λ°©λ² (λΉλκΈ°μ μΌλ‘ μνλλ―λ‘, λλ¨μ μ리λ λ°©λ²)
- completion handler - task κ° λλ λ μ€νλ¨
- delegate μ method νΈμΆ
URL μ ꡬμ±νλ μμλ€μ κ΅¬μ‘°μ²΄λ‘ λνλ
- queryItem property : URLQueryItem (name -value μ§μΌλ‘ ꡬμ±λμ΄ URL μ query λΆλΆμ λ΄λΉ)
- url property : ꡬμ±μμλ€λ‘λΆν° μμ±λ URL
λ°μ΄ν°λ₯Ό URL λ‘λΆν° κ°μ Έμ€λ €λ©΄
- μ΄λ€ λ°μ΄ν°λ₯Ό μ£ΌμΈμ : request
- μλ΅ : reponse
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()
λΉλκΈ° μνμ μν λ, 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 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 λ‘ μν΅ν μ μλ€λ λ»μ΄λ€.
- 보μ¬μ€ view controller μΈμ€ν΄μ€λ₯Ό μμ±νμ¬
μ±μ 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 λ₯Ό λ±λ‘νλ©΄ λλ€.