属性 'sharedInstance' 未在类类型的对象上找到
Property 'sharedInstance' not found on object of type ClassA
我正在创建一个 swift 框架。其中class是这样的,如下图
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
}
}
现在,当我将此框架添加到 Objective c 项目并尝试访问 "sharedInstance" 时,我收到此错误。
属性 'sharedInstance' 未在 ClassA 类型的对象上找到。
修复 将 'sharedInstance' 替换为 'sharedInstance'
但即使我尝试使用 Fix it,这个问题也没有解决。
注意: 当我将此框架与 swift 项目集成时,不会发生此问题!!!
我卡住了..:(
我试图重现你的问题。起初,Xcode 中的语法突出显示在 Objective-C 中标记了与您提到的相同的错误,但代码实际上已构建并且 运行 正常。
但是,有一种更简洁的方法可以做到这一点。在您的代码中,您使用的是计算类型 属性,每次访问它时都会对其进行评估!您可以通过引入 struct Static
来解决这个问题,您基本上可以在 classA
本身中完成,就像这样:
/**
Singleton intance is returned.
*/
public static var sharedInstance: classA = popeye()
这里我们使用了存储类型属性,这是一种推荐的实现单例的方式,见这里:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html
这里有一些关于不同类型属性的文档:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html
最后我可以通过一个小改动来解决这个问题!! :)
Swift框架代码
@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父项目代码
SingletonTest *aTest = [SingletonTest sharedInstance];
NSLog(@"Singleton says: %@", [aTest testTheSingleton]);
我正在创建一个 swift 框架。其中class是这样的,如下图
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
}
}
现在,当我将此框架添加到 Objective c 项目并尝试访问 "sharedInstance" 时,我收到此错误。
属性 'sharedInstance' 未在 ClassA 类型的对象上找到。 修复 将 'sharedInstance' 替换为 'sharedInstance'
但即使我尝试使用 Fix it,这个问题也没有解决。
注意: 当我将此框架与 swift 项目集成时,不会发生此问题!!!
我卡住了..:(
我试图重现你的问题。起初,Xcode 中的语法突出显示在 Objective-C 中标记了与您提到的相同的错误,但代码实际上已构建并且 运行 正常。
但是,有一种更简洁的方法可以做到这一点。在您的代码中,您使用的是计算类型 属性,每次访问它时都会对其进行评估!您可以通过引入 struct Static
来解决这个问题,您基本上可以在 classA
本身中完成,就像这样:
/**
Singleton intance is returned.
*/
public static var sharedInstance: classA = popeye()
这里我们使用了存储类型属性,这是一种推荐的实现单例的方式,见这里: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html 这里有一些关于不同类型属性的文档: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html
最后我可以通过一个小改动来解决这个问题!! :)
Swift框架代码
@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父项目代码
SingletonTest *aTest = [SingletonTest sharedInstance]; NSLog(@"Singleton says: %@", [aTest testTheSingleton]);