@selector() in Swift?

后端 未结 23 2563
清酒与你
清酒与你 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:02

    Selectors are an internal representation of a method name in Objective-C. In Objective-C "@selector(methodName)" would convert a source-code method into a data type of SEL. Since you can't use the @selector syntax in Swift (rickster is on point there), you have to manually specify the method name as a String object directly, or by passing a String object to the Selector type. Here is an example:

    var rightBarButton = UIBarButtonItem(
        title: "Logout", 
        style: UIBarButtonItemStyle.Plain, 
        target: self, 
        action:"logout"
    )
    

    or

    var rightBarButton = UIBarButtonItem(
        title: "Logout", 
        style: UIBarButtonItemStyle.Plain, 
        target: self, 
        action:Selector("logout")
    )
    

提交回复
热议问题