Abin Anto Abin Anto - 23 days ago 6
Objective-C Question

Property 'sharedInstance' not found on object of type ClassA

I am creating a swift framework. In that one class is like this as shown below.

import Foundation
@objc public class classA: NSObject {

public override init (){
super.init();
}

/**
Singleton intance is returned.
*/
public class var sharedInstance: classA {
struct Static {
static let instance = popeye();
}
return Static.instance
}
}


Now when i add this framework into a Objective c project and try to access "sharedInstance" i get this error.

Property 'sharedInstance' not found on object of type ClassA.
Fix it Replace 'sharedInstance' with 'sharedInstance'


But even if i try use Fix it, this issue isnt solved.

NOTE: This issue doesn't happen when i integrate this framework with a swift project!!!

I AM STUCK.. :(

Answer

Finally i was able to fix this with a minor change !! :)

  • Swift framework code

    @objc class SingletonTest: NSObject {

    // swiftSharedInstance is not accessible from ObjC
    class var swiftSharedInstance: SingletonTest {
    struct Singleton {
        static let instance = SingletonTest()
        }
        return Singleton.instance
    }
    
    // the sharedInstance class method can be reached from ObjC
    class func sharedInstance() -> SingletonTest {
        return SingletonTest.swiftSharedInstance
    }
    
    // Some testing
    func testTheSingleton() -> String {
        return "Hello World"
    }
    

    }

  • Objective C parent project code

    SingletonTest *aTest = [SingletonTest sharedInstance]; NSLog(@"Singleton says: %@", [aTest testTheSingleton]);