*Computed Property: ๋ค๋ฅธ ์์ฑ ๊ฐ์ผ๋ก ํด๋น ์์ฑ์ ๊ฐ์ ์ ์ํ๋ ๊ฒ.
์๋ condition ID ๊ฐ์ผ๋ก condition Name์ ๋ฐํํ๋ ํจ์๊ฐ ์๋ค. ์ด ๋, condition Name์ ๋ฐํํ๋ ค๋ฉด weatherData๋ฅผ ์ ์ธ ํ ํจ์๋ก ์ ๊ทผํด์ผํ ๊ฒ์ด๋ค.
์ด ๋ถ๋ถ์ computed Property๋ก ๋ฐ๊พธ๋ฉด watherData ์ ์ธ ์ฆ์ condition Name์ ๊ฐ๋ ์ ์ธ๋์ด ๋ฐ๋ก ์ฌ์ฉํ ์ ์๋ค.
์ฐธ๊ณ ๋ก ๊ทธ๋ฅ ์ ์ธํ conditionId, cityName ๊ฐ์ stored Property๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ฐจ์งํ์ง๋ง, computed Property๋ ์ ์ฅ๊ณต๊ฐ์ ์ฐจ์ง ํ์ง ์๋๋ค. ๋ ๊ณ์ ๊ฐ์ด ๋ฌ๋ผ์ง๋ฏ๋ก var ํ์ผ๋ก ์ ์ธํด์ผ ํ๋ค.
import Foundation
struct WeatherModel{
let conditionId: Int
let cityName: String
let temperature: Double
func getWeatherConditionName(id: Int) -> String {
switch id {
case 200...232:
return "cloud.bolt"
case 300...321:
return "cloud.drizzle"
case 500...531:
return "cloud.rain"
case 600...622:
return "cloud.snow"
case 701...781:
return "cloud.fog"
case 800:
return "sun.max"
case 801...804:
return "cloud.bolt"
default:
return ""
}
}
}
[Computed Property ์ ์ฉํ ์ฝ๋]
import Foundation
struct WeatherModel{
let conditionId: Int
let cityName: String
let temperature: Double
var conditionName: String{
switch conditionId {
case 200...232:
return "cloud.bolt"
case 300...321:
return "cloud.drizzle"
case 500...531:
return "cloud.rain"
case 600...622:
return "cloud.snow"
case 701...781:
return "cloud.fog"
case 800:
return "sun.max"
case 801...804:
return "cloud.bolt"
default:
return ""
}
}
}
conditionName ์ ์ธ ํ conditionName ๋ฐํ ํจ์๋ฅผ ๋ณ์์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
'๐ฑ App Development Study > iOS๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Udemy iOS & Swift Bootcamp] extension (0) | 2023.05.14 |
---|---|
[Udemy iOS & Swift Bootcamp] Internal and External Parameter Names (0) | 2023.05.13 |
[Udemy iOS & Swift Bootcamp] API ๋ก Json ๋ฐ์ดํฐ ๋ฐ์์ค๊ธฐ. Json Parsing (0) | 2023.05.06 |
[Udemy iOS & Swift Bootcamp] Closure(ํด๋ก์ ) (0) | 2023.04.29 |
[Udemy iOS & Swift Bootcamp] Delegate Design Pattern (0) | 2023.04.25 |