@selector() in Swift?

后端 未结 23 2562
清酒与你
清酒与你 2020-11-21 15:24

I\'m trying to create an NSTimer in Swift but I\'m having some trouble.

NSTimer(timeInterval: 1, target: self, selector: test(), us         


        
23条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 16:01

    selector is a word from Objective-C world and you are able to use it from Swift to have a possibility to call Objective-C from Swift It allows you to execute some code at runtime

    Before Swift 2.2 the syntax is:

    Selector("foo:")
    

    Since a function name is passed into Selector as a String parameter("foo") it is not possible to check a name in compile time. As a result you can get a runtime error:

    unrecognized selector sent to instance
    

    After Swift 2.2+ the syntax is:

    #selector(foo(_:))
    

    Xcode's autocomplete help you to call a right method

提交回复
热议问题