I have an obj-c class that uses a factory method to instantiate itself as a singleton. I added that class to my Swift bridge header and want to call this factory method from a swift class. However, XCode won't let me.
我有一个obj-c类,它使用工厂方法将自身实例化为单例。我将该类添加到我的Swift桥接头文件中,并希望从swift类中调用此工厂方法。但是,XCode不会让我。
The obj-c code is:
obj-c代码是:
@interface MOAssistant : NSObject {
...
+ (MOAssistant *)assistant;
@end
The Swift code is:
Swift代码是:
let assistant = MOAssistant.assistant;
With this code I get the error:
使用此代码我收到错误:
'assistant()' is unavailable: use object construction 'MOAssistant()'
'assistant()'不可用:使用对象构造'MOAssistant()'
I read about mapping factory methods to Swift initializers but I wonder why I simply can use:
我读到了将工厂方法映射到Swift初始化器,但我想知道为什么我只能使用:
let fm = NSFileManager.defaultManager;
even though defaultManager is also a factory method. This is confusing. I read other postings here on SO like these:
即使defaultManager也是工厂方法。这令人困惑。我在这里阅读其他帖子,如下所示:
- How are Objective-C factory methods converted into Swift convenience initializers?
- Create Swift instance from Objective-C factory method
Objective-C工厂方法如何转换为Swift便利初始化器?
从Objective-C工厂方法创建Swift实例
but none of them explain why Swift behaves differently. Using the recommended way (MOAssistant()
) is no solution as it directly calls the initializer of that class (what I don't want).
但他们都没有解释为什么斯威夫特行为不同。使用推荐的方式(MOAssistant())是没有解决方案,因为它直接调用该类的初始化程序(我不想要的)。
1 个解决方案
#1
3
As so often: explain your problem well and you may find an answer yourself. It's a matter of naming. By renaming the factory method to something else like defaultAssistant or something unrelated like makeThisInstanceForMe I can easily access it from Swift. Looks like the swift header creator removes the uppercase letters to see if that is the same as the function name (not case sensitive) and if that is the case makes it into a Swift initializer call instead of a class function.
经常这样:很好地解释你的问题,你可以自己找到答案。这是一个命名问题。通过将工厂方法重命名为defaultAssistant之类的东西,或者像makeThisInstanceForMe那样无关的东西,我可以从Swift轻松访问它。看起来swift标头创建者删除大写字母以查看它是否与函数名称相同(不区分大小写),如果是这种情况则使其成为Swift初始化程序调用而不是类函数。
However, keep in mind to call the factory method as method/function (including the parentheses) otherwise you get something else, but not what you expect. If you specify a type you can actually not call it without.
但是,请记住将工厂方法作为方法/函数(包括括号)调用,否则您将得到其他内容,但不是您所期望的。如果指定了类型,则实际上不能在没有的情况下调用它。
LLDB output when you don't use parentheses:
不使用括号时的LLDB输出:
(lldb) po assistant
(lldb)po助手
(Pecunia`_TPA__TTOFCSo11MOAssistant8makeThisfMS_FT_GSQS__ at RemoteResourceManager.swift)
(Pecunia`_TPA__TTOFCSo11MOAssistant8makeThisfMS_FT_GSQS__在RemoteResourceManager.swift)
This problem actually unveils another dangerous problem which you get if you use automatic/weak typing. There's a reason why weakly type languages are considered dangerous, even though they are convenient.
这个问题实际上揭示了如果你使用自动/弱键入你会得到另一个危险的问题。弱类型语言被认为是危险的,这是有原因的,即使它们很方便。
#1
3
As so often: explain your problem well and you may find an answer yourself. It's a matter of naming. By renaming the factory method to something else like defaultAssistant or something unrelated like makeThisInstanceForMe I can easily access it from Swift. Looks like the swift header creator removes the uppercase letters to see if that is the same as the function name (not case sensitive) and if that is the case makes it into a Swift initializer call instead of a class function.
经常这样:很好地解释你的问题,你可以自己找到答案。这是一个命名问题。通过将工厂方法重命名为defaultAssistant之类的东西,或者像makeThisInstanceForMe那样无关的东西,我可以从Swift轻松访问它。看起来swift标头创建者删除大写字母以查看它是否与函数名称相同(不区分大小写),如果是这种情况则使其成为Swift初始化程序调用而不是类函数。
However, keep in mind to call the factory method as method/function (including the parentheses) otherwise you get something else, but not what you expect. If you specify a type you can actually not call it without.
但是,请记住将工厂方法作为方法/函数(包括括号)调用,否则您将得到其他内容,但不是您所期望的。如果指定了类型,则实际上不能在没有的情况下调用它。
LLDB output when you don't use parentheses:
不使用括号时的LLDB输出:
(lldb) po assistant
(lldb)po助手
(Pecunia`_TPA__TTOFCSo11MOAssistant8makeThisfMS_FT_GSQS__ at RemoteResourceManager.swift)
(Pecunia`_TPA__TTOFCSo11MOAssistant8makeThisfMS_FT_GSQS__在RemoteResourceManager.swift)
This problem actually unveils another dangerous problem which you get if you use automatic/weak typing. There's a reason why weakly type languages are considered dangerous, even though they are convenient.
这个问题实际上揭示了如果你使用自动/弱键入你会得到另一个危险的问题。弱类型语言被认为是危险的,这是有原因的,即使它们很方便。