2

I'm a bit new to Swift programming so forgive me if this seems silly. I have a class outside my ViewController that I want to instantiate inside the ViewController, but I am getting an error:

initializer is inaccessible due to 'internal' protection level

The class:

public class TappyBleScanner : NSObject, CBCentralManagerDelegate {

      public var manager : CBCentralManager = CBCentralManager(delegate: self, queue : nil)

      public init(){} //based on other posts, I thought this would solve it
}

The code in the ViewController:

    public func scanForTappyBle() {

    var tappyScanner : TappyBleScanner = TappyBleScanner() //<- This is where Xcode 9 reports the error


}

Other posts here and here suggest that making an empty public initializer explicitly would solve this, but the error persists. The Swift documentation seems to suggest that the public no argument init() function would have allowed external modules to access the initializer and hence instantiate this class:

If you want a public type to be initializable with a no-argument initializer when used in another module, you must explicitly provide a public no-argument initializer yourself as part of the type’s definition.

However the error still persists, any ideas?

Papyrus
  • 232
  • 3
  • 10

1 Answers1

0

I couldn't even compile what you had (Xcode 9.2). You need to override init(). And you can't use self in a static initialization of manager.

If you change your TappyBleScanner to the following, it'll compile and run. :)

public class TappyBleScanner : NSObject, CBCentralManagerDelegate
{
    public var manager : CBCentralManager?

    override init()
    {
        super.init()
        self.manager = CBCentralManager(delegate: self, queue : nil)
    }

    public func centralManagerDidUpdateState(_ central: CBCentralManager)
    {
    }
}
Smartcat
  • 2,834
  • 1
  • 13
  • 25
  • Thanks! Yes I provided just a code snippett so there's not too much to look at. I originally used the overide init() like that and the same error was produced, even tried marking it public explicitly and no luck. I suspect now there's something wrong with the project configuration because this morning it seems to not even be finding the dependencies at all (undeclared type errors and such), and I didn't change anything except reboot. I'm new to XCode too. I'll get the dependencies/references working again and try your suggestion – Papyrus Mar 16 '18 at 11:56