๐Ÿ“ฑ App Development Study/iOS๐ŸŽ

[Udemy iOS & Swift Bootcamp] extension

ibelieveinme 2023. 5. 14. 00:18
728x90

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)
    }
}

 

728x90