extension ?
๊ธฐ์กด ํด๋์ค, ๊ตฌ์กฐ์ฒด, ์ด๊ฑฐํ ํ์ ์ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ ๊ฒ.
Swift๊ฐ ์คํ์์ค๊ฐ ์๋๋ผ์ ๊ธฐ์กด ์์ค์ฝ๋์์ ์ ๊ทผํ์ง ๋ชปํ๋ ํ์ ๋ค์ ํ์ฅํ ์ ์๋ค.
์ฝ๋ ๊ธฐ๋ณธํ
extension someType{
//Add new functionality
}
์์ 1)
import UIKit
var myDouble = 3.14519
let myRoundedDouble = String(format: "%.1f", myDouble) // 3.1
myDouble.round() //3
//myDouble.round(to: 3) //???? round์ to๊ฐ์ ๋ฃ๋ ํจ์๋ ์์ผ๋ฏ๋ก error๋ฅผ ๋ฐ์์ํจ๋ค.
3.14519 ๋ผ๋ ๊ฐ์ ๊ฐ์ง myDouble ๋ณ์๊ฐ ์๋ค.
์ด ๊ฑธ String ํจ์๋ฅผ ์ด์ฉํด์ ์์์ 1์ ์๋ฆฌ๊น์ง๋ง ์ถ๋ ฅํ ์ ์๋ค.
round() ํจ์๋ฅผ ์ด์ฉํ๋ฉด ์ ์๋ง ์ถ๋ ฅํ ์๋ ์๋ค.
์ด ๋, myDouble.round(to:3) ์ด๋ ๊ฒ ์ฌ์ฉํ ์๋ ์์๊น?
round ํจ์์๋ to ๊ฐ์ ๋ณด๋ด๋ ํจ์๋ ์๋ค. ๊ทธ๋์ error ๊ฐ ๋๋ค.
ํ์ง๋ง extension ์ผ๋ก ์ง์ ๋ง๋ ๋ค๋ฉด myDouble.round(to:3)์ ์ฌ์ฉํ ์๋ ์๋ค.
import UIKit
extension Double{
//(์ธ๋ถ์ธ์ ๋ด๋ถ์ธ์ : ํ์
) -> ๋ฆฌํดํ์
func round(to places: Int) -> Double
{
let precisionNumber = pow(10, Double(places))
var n = self
n = n * precisionNumber
n.round()
n = n / precisionNumber
return n
}
}
var myDouble = 3.14519
myDouble = myDouble * 1000
myDouble.round()
myDouble = myDouble / 1000
myDouble.round(to: 1)
Double extension ์ round(to places: Int) -> Double ํจ์๋ฅผ ๋ง๋ค์๋ค.
์ด์ myDouble.round(to: 1) ์๋ฐ ํ์์ด ๊ฐ๋ฅํ๋ค.
myDouble.round(to:1) ์ 1์ ์๋ฆฌ์๊น์ง ๋ฐํํ๋ ๊ฒ!
์์2)
๋ฒํผ ํจ์์๋ extension ์ ๋ง๋ค ์ ์๋ค.
์๋ ์ฝ๋๋ก UIButton์ ๋ง๋ค๊ณ ๋ฐฐ๊ฒฝ์์ ์ ํ์ค ๋ชจ์ต์ด๋ค.
๊ทธ๋ฅ ๋ฒํผ ๋ง๊ณ ๋ฅ๊ทผ ๋ฒํผ์ ๋ง๋ค๊ณ ์ถ์๋ฐ, ๊ทธ๋ฐ ํจ์๋ ์๋ค. ๊ทผ๋ฐ extension ์ผ๋ก ๋ง๋ค๋ฉด ๋๋ค.
UIButton extension ์ ๋ง๋ค๊ณ ๊ทธ ์์ makeCircular() ํจ์๋ฅผ ์ถ๊ฐํ๋ค.
button์ makeCircular() ํจ์๊ฐ ์๊ธฐ๊ณ ์ด๋ฅผ ํธ์ถํ๋ฉด ์์ ๊ฐ์ด ๋ฅ๊ทผ ๋ฒํผ์ด ๋ง๋ค์ด ์ง๋ค.
๊ทธ ์ธ ํ๋กํ ์ฝ๋ ๊ฐ๋ฅ !
Clima ํ๋ก์ ํธ์ ์ ์ฉ
//์ด์ ์ฝ๋
import UIKit
class WeatherViewController: UIViewController, UITextFieldDelegate, WeatherManagerDelegate {
@IBOutlet weak var conditionImageView: UIImageView!
@IBOutlet weak var temperatureLabel: UILabel!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var searchTextField: UITextField!
var weatherManager = WeatherManager()
override func viewDidLoad() {
super.viewDidLoad()
weatherManager.delegate = self
searchTextField.delegate = self
}
@IBAction func serarchPressed(_ sender: UIButton) {
searchTextField.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
searchTextField.endEditing(false)
print(searchTextField.text!)
return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if textField.text != ""{
return true
}
else {
textField.placeholder = "Type Something"
return false
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if let city = searchTextField.text {
weatherManager.fetchWeather(cityName: city)
}
searchTextField.text = ""
}
func didUpdateWeather(_ weatherManger: WeatherManager, weather: WeatherModel){
//DispatchQueue ๋ก ์๊ธฐ๋ค๋ฆฌ๋ฉด ์๋ฌ ๋จ.
DispatchQueue.main.async{
self.temperatureLabel.text = weather.temperatureName
self.conditionImageView.image = UIImage(systemName: weather.conditionName)
}
print(weather.temperature)
}
func didFailWithError(error: Error){
print(error)
}
}
//extension์ ์ ์ฉํด์ ์ฝ๋๋ฅผ ๋จ์ํ ํ ๊ฒ.
import UIKit
class WeatherViewController: UIViewController {
@IBOutlet weak var conditionImageView: UIImageView!
@IBOutlet weak var temperatureLabel: UILabel!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var searchTextField: UITextField!
var weatherManager = WeatherManager()
override func viewDidLoad() {
super.viewDidLoad()
weatherManager.delegate = self
searchTextField.delegate = self
}
}
extension WeatherViewController: UITextFieldDelegate{
@IBAction func serarchPressed(_ sender: UIButton) {
searchTextField.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
searchTextField.endEditing(false)
print(searchTextField.text!)
return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if textField.text != ""{
return true
}
else {
textField.placeholder = "Type Something"
return false
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if let city = searchTextField.text {
weatherManager.fetchWeather(cityName: city)
}
searchTextField.text = ""
}
}
extension WeatherViewController: WeatherManagerDelegate{
func didUpdateWeather(_ weatherManger: WeatherManager, weather: WeatherModel){
//DispatchQueue ๋ก ์๊ธฐ๋ค๋ฆฌ๋ฉด ์๋ฌ ๋จ.
DispatchQueue.main.async{
self.temperatureLabel.text = weather.temperatureName
self.conditionImageView.image = UIImage(systemName: weather.conditionName)
}
print(weather.temperature)
}
func didFailWithError(error: Error){
print(error)
}
}
'๐ฑ App Development Study > iOS๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Apple Property Lists Document (0) | 2023.05.17 |
---|---|
[Udemy iOS & Swift Bootcamp] ํ์ฌ ์์น ์ ๋ณด ์ฌ์ฉํ๊ธฐ (0) | 2023.05.15 |
[Udemy iOS & Swift Bootcamp] Internal and External Parameter Names (0) | 2023.05.13 |
[Udemy iOS & Swift Bootcamp] Computed Property(์ฐ์ฐ ํ๋กํผํฐ) (0) | 2023.05.07 |
[Udemy iOS & Swift Bootcamp] API ๋ก Json ๋ฐ์ดํฐ ๋ฐ์์ค๊ธฐ. Json Parsing (0) | 2023.05.06 |