@selector() in Swift?

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

    Using #selector will check your code at compile time to make sure the method you want to call actually exists. Even better, if the method doesn’t exist, you’ll get a compile error: Xcode will refuse to build your app, thus banishing to oblivion another possible source of bugs.

    override func viewDidLoad() {
            super.viewDidLoad()
    
            navigationItem.rightBarButtonItem =
                UIBarButtonItem(barButtonSystemItem: .Add, target: self,
                                action: #selector(addNewFireflyRefernce))
        }
    
        func addNewFireflyReference() {
            gratuitousReferences.append("Curse your sudden but inevitable betrayal!")
        }
    

提交回复
热议问题