0

I've faced this issue many many times, and I can't wrap my head around what's wrong with it.

Here's the code :

struct dataSource {
    let menu = MenuView()
    static var array = NSArray(contentsOfFile: Bundle.main.path(forResource: "Australie", ofType: "plist")!)!

    init() {
    }

    func getDicoSection(sectionNumber: Int) -> NSDictionary {
        var dico = NSDictionary()
        dico = dataSource.array[sectionNumber] as! NSDictionary
        return dico
    }

    func getNumberOfRootSections() -> Int {
        return dataSource.array.count
    }

    func setMenu(delegate: MenuViewDelegate, menuView: MenuView, rootView: UIView, navigationController: UINavigationController, navigationItem: UINavigationItem) {
        let menuButton = UIBarButtonItem(title: "test", style: .plain, target: self, action: #selector(dataSource.menuButtonSelected))
        navigationController.navigationBar.isTranslucent = false // permet de faire en sorte que la view ne soit pas derrière la navigationBar
        navigationController.navigationBar.barTintColor = UIColor.red
        navigationItem.rightBarButtonItem = menuButton
        print(menuButton)
        MenuView().delegate = delegate
        rootView.addSubview(menu)
        menu.backgroundColor = UIColor.lightGray
    }

    func menuButtonSelected() {
        print("OK")
    }
}

On my button I add a selector for the action to call when it's pressed. Which is a simple print("OK").

However, Swift asks me to put @objc in front of my method. Why ? I have absolutely no idea why. But I'm resilient with XCode, so I obey. I add @objc. But as you can see, it doesn't resolve anything at all : https://drive.google.com/open?id=0B-AByLCqlbVBcG95SFhTOE1sMzA

I think I'm going to need to go to a mental clinic after that.

petaire
  • 495
  • 1
  • 10
  • 23
  • 1
    The target needs to be an instance of a *class* which inherits from `NSObject`. Compare e.g. https://stackoverflow.com/questions/24415662/object-x-of-class-y-does-not-implement-methodsignatureforselector-in-swift. – The error message `@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes` is actually a good hint from the compiler. – Martin R Jun 10 '17 at 11:13
  • But can't we add a swift action to a button ? It seems kind of crazy not being able to do that ! – petaire Jun 10 '17 at 11:23
  • @Kenneth I guess you can somehow implement `NSObjectProtocol` – Sweeper Jun 10 '17 at 11:32
  • You CAN attach an action to a button. That's the whole point. If you're using a selector, which isn't really a swift construct, you are calling an objective c method. So the button needs to be a UIButton. It's really about the selector. – Mozahler Jun 10 '17 at 12:46
  • 2
    How about you change `dataSource` to an ObjC class: `class dataSource : NSObject`. The `menuButtonSelected` function must be available to the ObjC runtime. And the way you do that is to expose its container (the class) to ObjC. – Code Different Jun 10 '17 at 13:01
  • Yeah that worked ! Thanks. I'm always feeling blue when I have to go back to objc things… – petaire Jun 10 '17 at 13:12

0 Answers0