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

[Udemy iOS & Swift Bootcamp] Closure(ํด๋กœ์ €)

ibelieveinme 2023. 4. 29. 20:59
728x90

* Closures

: ๋žŒ๋‹ค์‹ ๊ฐ™์€๊ณ .

: Closure๋Š” named Closure๊ณผ unnamed Closure๊ฐ€ ์žˆ๋‹ค. named Closure๊ฐ€ ์ผ๋ฐ˜ swift ํ•จ์ˆ˜๋ฅผ ๋งํ•˜๊ณ , unnamed Closure๋Š” ์ด๋ฆ„์—†์ด ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ Closure๋ผ๊ณ  ํ•˜๋ฉด unnamed Closure๋ฅผ ๋œปํ•œ๋‹ค.

 

์•„๋ž˜ completionHandler๋กœ ์‚ฌ์šฉํ•œ handle ํ•จ์ˆ˜๋ฅผ ๋ณด์ž.

handle ํ•จ์ˆ˜๋ฅผ func ํ•จ์ˆ˜๋กœ ๋ช…์‹œํ•˜์—ฌ ๋”ฐ๋กœ ์ •์˜ํ•ด์ฃผ์—ˆ๋‹ค. ์ด๋Ÿฐ๊ฑธ named Closure๋ผ๊ณ  ํ•œ๋‹ค.

 

์ด๊ฑธ Closure(unnamed Closure)๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋œ๋‹ค.

ํ•จ์ˆ˜๋ช…์„ ์—†์• ๊ณ  ์ธ์ž๋งŒ ๋‚จ๊ธด ํ›„, in ์•ˆ์— handle ํ•จ์ˆ˜์— ์žˆ๋˜ ๋‚ด์šฉ์„ ์ ์–ด์คฌ๋‹ค.

 

์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜๋ช… ์—†์ด ์ธ์ž, ๋ฆฌํ„ด, ํ•จ์ˆ˜๋‚ด์šฉ์„ ์ง์ ‘ ์ ์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ Closure๋ผ๊ณ  ํ•œ๋‹ค.

๋žŒ๋‹ค์‹์ฒ˜๋Ÿผ ์ฝ”๋“œ ๊ฐ„๊ฒฐ์„ฑ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ž˜์•Œ์•„๋‘์ž.

 

 

* Closure ๊ธฐ๋ณธ ํ˜•ํƒœ

{(Parameters) -> Return Type in
    //์‹คํ–‰๊ตฌ๋ฌธ
}

 

* Clima ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•œ ์ „์ฒด ์ฝ”๋“œ

import Foundation

struct WeatherManager{
    
    let weatherURL = "https://api.openweathermap.org/data/2.5/weather?appid=7722c26a2cee69ed24d77d3f57d3daf3&units=metric"
    
    func fetchWeather(cityName: String){
        let urlString = "\(weatherURL)&q=\(cityName)"
        performRequest(urlString: urlString)
    }
    
    func performRequest(urlString: String){
        //1. Create a URL
        if let url = URL(string: urlString){
            
            //2. Create a URLSession
            let session = URLSession(configuration: .default)
            
            //3. Give the session a task
            let task = session.dataTask(with: url, completionHandler: handle(data : response: error: ))
            
            //4. Start the task
            task.resume()
        }
    }
    
    func handle(data: Data?, response: URLResponse?, error: Error?){
        if error != nil {
            print(error!)
            return
        }
        
        if let safeData = data {
            let dataString = String(data: safeData, encoding: .utf8)
            print(dataString)
        }
    }
}

performRequest ํ•จ์ˆ˜์˜ session.dataTask handler ๋ถ€๋ถ„์„ Closure ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ”๋ณด์ž.

 

import Foundation

struct WeatherManager{
    
    let weatherURL = "https://api.openweathermap.org/data/2.5/weather?appid=7722c26a2cee69ed24d77d3f57d3daf3&units=metric"
    
    func fetchWeather(cityName: String){
        let urlString = "\(weatherURL)&q=\(cityName)"
        performRequest(urlString: urlString)
    }
    
    func performRequest(urlString: String){
        //1. Create a URL
        if let url = URL(string: urlString){
            
            //2. Create a URLSession
            let session = URLSession(configuration: .default)
            
            //3. Give the session a task
            let task = session.dataTask(with: url){ (data, response, error) in
                if error != nil {
                    print(error!)
                    return
                }
                if let safeData = data {
                    let dataString = String(data: safeData, encoding: .utf8)
                    print(dataString)
                }
            }
            //4. Start the task
            task.resume()
        }
    }
}

ํ›จ์”ฌ ์ฝ”๋“œ๊ฐ€ ์งง์•„์ง€๊ธด ํ–ˆ๋Š”๋ฐ, ๊ฐ€๋…์„ฑ ์ธก๋ฉด์„ ๊ณ ๋ คํ•ด์„œ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

728x90