简体   繁体   中英

singleton in framework with error: initializer is inaccessible due to 'private' protection level

So, I made a framework in swift and at first I wanted to use a singleton class. I built it and put the .Framework file into a new project to test it. Than I got this error:

'getInstance' is inaccessible due to 'internal' protection level

. I tried looking for anyone with the same problem, but nothing I found worked. It might be because its a framework. After hours of meaningless searching, I gave up on the singleton and I got almost the same error with a normal class.

'mySDK' initializer is inaccessible due to 'private' protection level

I tried making the class public, the initializer public, but nothing seems to change. Anyone experienced any problem like this? I never worked on frameworks before, so maybe its the obj-c header that have to be modified. If you need any more information, please just ask.

Thank you all, in advance.

Edit:

This is the getInstance func. I wrote it only, because the mySDK.myInstance seemed to give the same error.

static let myInstance = mySDK()

public static func getInstance() -> mySDK {
        return myInstance
    }

I don't know what caused the error, but I managed to fix it by creating a new project, than copy pasting the code from the old to the new.

I found the source of the problem. If I turned off the build active architecture only option in the build settings of the framework, It gave me this error.

You can create a singleton class in a framework like this. Please make the public Class and public static getInstance function.

public class ClassName {

// Singleton instance
private static var instance         : ClassName?

/********************* Singleton Instance ************************************/

public static func getInstance() -> ClassName {

    if (instance != nil) {
        return instance!
    }

    // Initialize instance.
    instance = ClassName()
    return instance!
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM