Udemy Angela Yu 강의 내용 중 --
computed property
var aProperty: Int {
return 2+5
}
결국 aProperty는 2+5와 같음.
응용: Clima 프로젝트 중
struct WeatherModel {
let conditionId: Int
let cityName: String
let temperature: Double
//소수점 한자리까지만 표시
var temperatureString: String {
return String(format: "%1f", temperature)
}
}
WeatherManager.swift 파일 내에서
let weather = WeatherModel(conditionId: id, cityName: name, temperature: temp)
print(weather.temperatureString) => 이런식으로 사용
WeatherManager.swift 파일 내에서
let weather.= WeatherModel(conditionId: id, cityName: name, temperatue: temp)
print(weather.getConditionName(weatherId: id)) 이런식으로 쓰던거를 간단하게
-> print(weather.conditionName)으로 쓸 수 있게 하는 방법? 아래와 같이 WeatherModel에서 적으면 됨.
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 "cloud"
}
}
해당 함수는 원래 아래와 같이 적었었음.
func getConditionName(weatherId: Int) -> String {
switch weatherId {
case 200...232:
return "cloud.bolt"
....
}
참고:
https://onelife2live.tistory.com/17
'iOS > iOS Swift Udemy - AngelaYu' 카테고리의 다른 글
Sec13. 154) 외부 및 내부 매개변수 (0) | 2023.01.24 |
---|---|
swift unwrapping 오류 관련 (0) | 2023.01.02 |
Sec13 149) Networking - URL Session (0) | 2022.12.27 |
Section13. 145) Protocol (0) | 2022.12.13 |
Section13 144) UITextField keyboard "go"클릭시 실행 (0) | 2022.12.13 |