ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Swift] μ…€λ ‰ν„°(Selector)λž€?
    iOS/🟠 Swift 2021. 5. 2. 16:00

    1μ°¨ μˆ˜μ • : 2021/06/16

    2μ°¨ μˆ˜μ •: 2021/07/05

    3μ°¨ μˆ˜μ •: 2021/07/08

    4μ°¨ μˆ˜μ •: 2022/02/13

     

    μ•ˆλ…•ν•˜μ„Έμš”πŸΆ

     

    이번 μ‹œκ°„μ—λŠ” 셀렉터에 λŒ€ν•΄ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€.

     

    항상 Objective-C 와 κ΄€λ ¨λœ κ°œλ…μ΄ λ‚˜μ˜€λ©΄ λ§‰μ—°νžˆ μ–΄λ €μ›Œμ§€λŠ” λŠλ‚Œμ΄ λ“œλŠ”λ°μš”....

     

    λ‹€ν–‰νžˆ μ…€λ ‰ν„°λŠ” κ·Έλ ‡κ²Œ μ–΄λ €μš΄ κ°œλ…μ€ μ•„λ‹™λ‹ˆλ‹€~~

     

     

    그럼 μ‹œμž‘ν•΄λ³Όκ²Œμš”!!πŸ”₯


     

    μ…€λ ‰ν„°λŠ” λ©”μ„œλ“œλ₯Ό 식별할 수 μžˆλŠ” κ³ μœ ν•œ μ΄λ¦„μž…λ‹ˆλ‹€.

    Swiftμ—μ„œλŠ” struct νƒ€μž…μ΄λ©° 컴파일 νƒ€μž„μ— μ§€μ •λ©λ‹ˆλ‹€.

     

    " A selector is the name used to select a method to execute for an object, or the unique identifier that replaces the name when the source code is compiled. A selector by itself doesn’t do anything. It simply identifies a method. The only thing that makes the selector method name different from a plain string is that the compiler makes sure that selectors are unique."

     

     

    μ–Έμ œ ν•„μš”ν• κΉŒ?

    UIKit λ‚΄λΆ€μ˜ Objective-C λŸ°νƒ€μž„μœΌλ‘œ μ‹€ν–‰λ˜λŠ” λ©”μ„œλ“œκ°€ μ…€λ ‰ν„°λ₯Ό νŒŒλΌλ―Έν„°λ‘œ 전달받을 λ•Œ,

    전달에 ν•„μš”ν•œ μ…€λ ‰ν„° μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λ €κ³  μ‚¬μš©ν•©λ‹ˆλ‹€.

    (μ΄μ™Έμ˜ μš©λ„λ‘œ μ‚¬μš©ν•΄λ³Έ 적이 μ—†μŠ΅λ‹ˆλ‹€....πŸ€”)

     

    예λ₯Ό λ“€μ–΄ μ•„λž˜ 사진과 같이 컨트둀의 target-action 을 κ΅¬ν˜„ν•  λ•Œ,

    addTarget(target:action:for) λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λŠ”λ°μš”....

    " μ—¬κΈ°μ„œ μ»¨νŠΈλ‘€μ€ UIControl λ₯Ό μ„œλΈŒν΄λž˜μ‹±ν•œ UIButton, UISlider λ“±μ˜ 클래슀 였브젝트λ₯Ό κ°€λ¦¬ν‚΅λ‹ˆλ‹€."

     

    이 λ©”μ†Œλ“œμ˜ λ‘λ²ˆμ§Έ μ „λ‹¬μΈμž action 은 λ°”λ‘œλ°”λ‘œ Selector νƒ€μž…μ„ λ°›κ³  μžˆμŠ΅λ‹ˆλ‹€!!

     

    이 λ•Œ μ…€λ ‰ν„°λ₯Ό λ§Œλ“€μ–΄μ„œ 전달해 μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€!!!!


     

    μ…€λ ‰ν„°λŠ” μ–΄λ–»κ²Œ λ§Œλ“œλ‚˜μš”?

     

    let selector = #selector(someFunction)

     

    Seletor νƒ€μž…μ΄ μ œκ³΅ν•˜λŠ” 기본적인 μƒμ„±μžκ°€ μžˆμ§€λ§Œ,

     #selctor( ) λ₯Ό μ‚¬μš©ν•˜μ—¬ μΈμŠ€ν„΄μŠ€λ₯Ό μ–»μŠ΅λ‹ˆλ‹€.

     

    이런 μ‹μœΌλ‘œ 말이죠

     @objc func makeDescription(age: Int, birthDate: Date) { 
    	// κ΅¬ν˜„λΆ€
     } 
     
     
     #selector(makeDescription(age:birthDate:))

     

    #selector( ) λ©”μ„œλ“œλŠ” νŒŒλΌλ―Έν„°μ— Function Notation λ°©μ‹μœΌλ‘œ ν•¨μˆ˜λ₯Ό μ „λ‹¬ν•©λ‹ˆλ‹€.

     

    >> Function Notaion (ν•¨μˆ˜ ν‘œκΈ°λ²•) 방식이 κΆκΈˆν•˜λ‹€λ©΄?? <<

     

     

    μš”λŸ°μ‹μœΌλ‘œ Argument Label 을 : (콜둠) 으둜 κ΅¬λΆ„ν•˜μ—¬ λ‚˜μ—΄ν•˜λŠ”κ²Œ Function Notaion 방식 μž…λ‹ˆλ‹€πŸ˜™

     


     

    μ…€λ ‰ν„°λ₯Ό 생성할 λ©”μ„œλ“œλŠ” @objc λ₯Ό λΆ™μ—¬μ•Όν•œλ‹€

    @objc func test() { print("test funtion") } let selector = #selector(test) button.addTarget(target: self, action: selector, for: .touchUpInside)

     

    #selector() 에 순수 swift ν•¨μˆ˜λ₯Ό μ „λ‹¬ν•˜λ©΄ μΈμ‹ν•˜μ§€ λͺ»ν•©λ‹ˆλ‹€.

     

    Objective-C 에 μΈμ‹μ‹œν‚€κΈ° μœ„ν•΄μ„œλŠ” μœ„ μ½”λ“œμ˜ @objc test() {...} 처럼

    λ©”μ„œλ“œ μ•žμ— @objc νŠΉμ„±μ„ μΆ”κ°€ν•΄μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€.

     

     

     

    참고둜, 클래슀 μ†μ„±μ˜ getter 와 setter 도 μ…€λ ‰ν„°λ₯Ό λ§Œλ“€μ–΄ 쀄 수 μžˆμŠ΅λ‹ˆλ‹€.

     

    이 λ•ŒλŠ” 속성 μ•žμ— @objc νŠΉμ„±μ„ μΆ”κ°€ν•΄μ€λ‹ˆλ‹€.

    import Foundation class Person { @objc var name: String init(name: String) { self.name = name } } var person = Person(name: "Park") let selector = #selector(getter: person.name)

     

     

     

    끝!


    πŸ€–πŸŸ’[우짱의 iOS λΈ”λ‘œκ·Έ]πŸ”΅πŸ’»

    iOSλ₯Ό κ³΅λΆ€ν•˜λ©΄μ„œ 배운 λ‚΄μš©μ„ κΈ°λ‘ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

     

    λŒ“κΈ€

μ–΄μ œλ³΄λ‹€ λ°œμ „ν•œ λ‚˜