โžฟ iOS Swift Closure & @escaping ํด๋กœ์ € ์ •๋ฆฌ

2022. 8. 26. 11:45ใ†Programming/Swift

https://www.youtube.com/watch?v=xiS5gJOIQxI 

https://dongminyoon.tistory.com/14

https://babbab2.tistory.com/81

https://babbab2.tistory.com/82

https://babbab2.tistory.com/83

 

 

 

 

 

// TODO: completion handler ์ดํ•ด

   func translatinEnglishToKorean(text: String, completion: @escaping () -> Void) {
        // text๋ž‘ completion์ด๋ผ๋Š” ํด๋กœ์ €๋ฅผ ๋ฐ›์„ ๊ฑฐ๊ณ , ๋’ค์˜ ๋‚ด์šฉ์€ ์ด ํ•จ์ˆ˜์˜ ์‹คํ–‰ ๊ณผ์ •, ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์— closure ์‹คํ–‰
        englishToKoreanTranslator.translate(text) { [self] translatedText, error in
            self.resultKoreanText = translatedText ?? ""
            completion()
        }
    }

 

    OpenAIManager.shared.makeRequest(json: jsonPayload) { [weak self] (str) in

        DispatchQueue.main.async {
            self?.mlKit.translatinEnglishToKorean(text: str) {
                self?.outputKoreanWord = self?.mlKit.resultKoreanText ?? ""
                self?.outputTextView.text = self?.outputKoreanWord
                // ์ด๊ฒŒ @escaping ํด๋กœ์ € (ํ•จ์ˆ˜ ์‹คํ–‰ ๋ฐ–์œผ๋กœ ๋‚˜์™€์„œ ํ•  ์ผ)
            }
            spinner.stopAnimating()
            spinner.removeFromSuperview()
            view.removeFromSuperview()
        }
    }

 

 

 

ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ถˆ๋Ÿฌ์˜จ ํด๋กœ์ €๋ฅผ ๋‹ค๋ฅธ ๊ณณ์—์„œ ํ˜ธ์ถœํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์—๋Ÿฌ (๋จผ์ € ์ฃฝ์œผ๋‹ˆ๊นŒ)

: ์ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ ค๊ณ  @escaping์„ ์„ ์–ธํ•ด ํƒˆ์ถœ ํด๋กœ์ €๋กœ ์‚ฌ์šฉ

 

 

 

 

    let yes = UIAlertAction(title: "์˜ˆ", style: .default, handler: { [weak self] _ in
        UserDefaults.standard.set("", forKey: "roomCode")
        UserDefaults.standard.set("", forKey: "name")
        UserDefaults.standard.set("", forKey: "userIdentifier")
        UserDefaults.standard.set("", forKey: "forUserID")
        self?.navigationController?.popToRootViewController(animated: true)
    })

 

- in ์œผ๋กœ ํด๋กœ์ €์˜ ํ—ค๋“œ / ๋ฐ”๋”” ๊ตฌ๋ถ„!

: head: (parameter, parameter, ... ) -> return type

 

- ์ „์—ญํ•จ์ˆ˜: ์ฃผ๋ณ€์˜ ์–ด๋– ํ•œ ๊ฐ’๋„ ์บก์ณํ•˜์ง€ ์•Š์Œ (๊ฑ ํŒŒ๋ผ๋ฏธํ„ฐ ๋„˜๊ธธ๋•Œ ๋ฐ›์€ ๊ฐ’๋งŒ ์‚ฌ์šฉํ•˜๊ณ , ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋“ฑ๋“ฑ)

- ์ค‘์ฒฉํ•จ์ˆ˜: ์ž์‹ ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜์˜ ๊ฐ’๋งŒ ์บก์ณ ๊ฐ€๋Šฅ (์ฝ”๋“œ ์ˆœ์„œ ์ƒ inner ํ•จ์ˆ˜๊ฐ€ outer ํ•จ์ˆ˜์˜ ๊ฐ’์„ ํ™œ์šฉํ•œ๋‹ค๋Š” ๋…ผ๋ฆฌ๊ฐ€ ๋‹น์—ฐํ•จ)

                : ํ•˜์ง€๋งŒ, ์–˜๋„ ํ•จ์ˆ˜ ๋ฐ”๊นฅ์—์„œ๋Š” ์ฃฝ์Œ (์ฝ”๋“œ ํƒˆ์ถœ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•จ -> ์ด๊ฒŒ escaping์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ )

- ์ต๋ช…ํ•จ์ˆ˜(๋‹ค ํด๋กœ์ €์ง€๋งŒ, ์ข์€ ์˜๋ฏธ๋กœ ํด๋กœ์ €)

 

- self์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ [Closure Capture Lists]๋ฅผ ์ด์šฉํ•˜์—ฌ weak, unowned๋กœ ์บก์ณ

์บก์ณ: ํด๋กœ์ €๊ฐ€ ํ™œ์šฉํ•˜๋Š” ์ฃผ๋ณ€ ๊ฐ’(parameter)๋“ค์„ ref๋กœ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ, ์ด๋ฅผ value๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ!

์ด๋•Œ ํด๋กœ์ € ๋‚ด๋ถ€์—์„œ value๋กœ ์บก์ณ๋œ ๊ฐ’์€ ๋ณ€๊ฒฝ์‹œํ‚ฌ ์ˆ˜ ์—†๋‹ค. (์ƒ์ˆ˜ ์ทจ๊ธ‰) 

 

- ํด๋กœ์ €๋Š” ref -> heap

 

- ์™œ [weak self]?

self๋ฅผ ์บก์ณํ•  ๋•Œ, self์— ๋Œ€ํ•œ ์ˆœํ™˜์ฐธ์กฐ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด weak self๋ฅผ ์‚ฌ์šฉ

: self ์™œ ์บก์ณ? ํด๋กœ์ € ๋‚ด๋ถ€์—์„œ self. ์ ‘๊ทผ ์‚ฌ์šฉํ•˜๋‹ˆ๊น,, 

https://babbab2.tistory.com/83 ๋” ์ฝ์–ด๋ณด๊ธฐ

 

 

 

 

์–ด ๋ ต ๋‹ค