我可以将Swift默认参数值公开给Objective-C吗? [英] Can I expose Swift default parameter values to Objective-C?

查看:98
本文介绍了我可以将Swift默认参数值公开给Objective-C吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一些Swift类,该类带有我想向Objective-C公开的方法:

Say I have some Swift class with methods I'd like to expose to Objective-C:

@objc(Worker) class Worker : NSObject {
    func performWork(label: String = "Regular Work") {
        // Perform work
    }
}

我可以用两种方式称呼它:

I can call this in two ways:

Worker().performWork()
Worker().performWork("Swift Development")

但是,从Objective-C调用时,我没有获得此方法的默认"版本-我必须提供标签:

However, when called from Objective-C, I do not get the "default" version of this method - I must supply a label:

[[[Worker alloc] init] performWork:@"Obj-C Development"];

从下面的AppName-Swift.h标头中查看定义:

See the definition from my AppName-Swift.h header below:

SWIFT_CLASS_NAMED("Worker")
@interface Worker : NSObject
- (void)performWork:(NSString * __nonnull)label;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

我会期望看到具有此签名的方法:

I would expect to see a method with this signature:

- (void)performWork;

我意识到ObjC不支持默认参数值,但是我的假设是Swift的默认值只是通过琐碎的实现调用"real"函数(即它们隐藏了典型实现.

I realise ObjC doesn't support default parameter values, but my assumption was that the Swift default values simply generate additional Swift functions with trivial implementations calling the "real" function (i.e. they're syntactic sugar hiding the typical implementation we'd use in Obj-C).

有什么办法可以将此默认值公开给Objective-C?如果没有,谁能指出我为什么不提供任何文档?

Is there any way to expose this default value to Objective-C? If not, can anyone point me to any documentation on why this isn't available?

推荐答案

答案是否定的,目前尚无办法公开.

The answer is no, there is currently no way to expose that.

然而,它正被考虑用于Swift 3.0(请参阅提案

It is however, being considered for Swift 3.0 (see the proposal here)

这是不正确的,正如Adam S指出的.

This is incorrect as pointed out by Adam S.

这篇关于我可以将Swift默认参数值公开给Objective-C吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆