与Objective C的Swift 2.0互操作不能按预期工作?(Swift 2.0 interop with Objective C not working as expected?)

编程入门 行业动态 更新时间:2024-10-27 19:22:54
与Objective C的Swift 2.0互操作不能按预期工作?(Swift 2.0 interop with Objective C not working as expected?)

我正在将Swift 1.2框架移植到2.0。 在修复了新的Swift 2.0错误处理方案的300万编译器错误之后,我终于能够链接我的测试应用程序(用Objective C编写)来使用更新的框架。

Xcode版本7.0 beta 3(7A121l)

但是,我遇到了一个问题。 一些Swift函数不再生成到Objective C测试应用程序使用的自动生成的Objective C头文件(MyFramework-Swift.h)中。

下面是一个未公开的函数示例:(我的实际框架函数返回了枚举,但我试图简化以更清楚地说明问题)。

public func calculateImportantValueDangerously() throws -> Int { return 42 }

请注意,像下面这样的其他函数实际上会按预期公开(并且可以调用):

public func doSomething() { } public func doSomethingDangerous() throws { } public func calculateMeaninglessValue() -> Int { return -1 }

这是Objective C方面:

MyClass *newInstance = [[MyClass alloc] init]; [newInstance doSomething]; NSError *error = nil; [newInstance doSomethingDangerousAndReturnError:&error]; long meaninglessValue = [newInstance calculateMeaninglessValue]; NSLog(@"%ld", meaninglessValue); long importantValue = [newInstance calculateImportantValueDangerouslyAndReturnError:&error]; <-COMPILE ERROR NSLog(@"%ld", importantValue);

从观看此视频开始,我原本预计它应该“正常工作”:

https://developer.apple.com/videos/wwdc/2015/?id=401

...但似乎我们目前无法使用抛出和返回值的函数。

这是一个错误,还是没有实现的功能? 如果我在某处发布说明中遗漏了某些内容,我表示歉意。

任何建议表示赞赏

I'm porting a Swift 1.2 framework to 2.0. After fixing the 3 million compiler errors courtesy of the new Swift 2.0 error handling scheme, I was finally able to link my test app (written in Objective C) to use the updated framework.

Xcode version 7.0 beta 3 (7A121l)

However, I ran into a problem. Some Swift functions are no longer generated into the automatically generated Objective C header (MyFramework-Swift.h) used by the Objective C test app.

Here is an example of a function that is not exposed: (My actual framework function returned an enum, but I tried to simplify to illustrate the problem more clearly).

public func calculateImportantValueDangerously() throws -> Int { return 42 }

Note that other functions like the following actually do get exposed as expected (and can be called):

public func doSomething() { } public func doSomethingDangerous() throws { } public func calculateMeaninglessValue() -> Int { return -1 }

Here's the Objective C side:

MyClass *newInstance = [[MyClass alloc] init]; [newInstance doSomething]; NSError *error = nil; [newInstance doSomethingDangerousAndReturnError:&error]; long meaninglessValue = [newInstance calculateMeaninglessValue]; NSLog(@"%ld", meaninglessValue); long importantValue = [newInstance calculateImportantValueDangerouslyAndReturnError:&error]; <-COMPILE ERROR NSLog(@"%ld", importantValue);

From watching this video, I had expected that it should "just work":

https://developer.apple.com/videos/wwdc/2015/?id=401

...but it seems like we can't currently use functions that both throw and return a value.

Is this a bug, or not implemented feature? My apologies if I missed something in the release notes somewhere.

Any advice appreciated.

最满意答案

这不可能。

如果您使用@objc注释您的方法,您将看到问题。

抛出方法不能标记为@objc,因为它返回一个'Int'类型的值; 返回'Void'或桥接到Objective-C类的类型

您只能返回对象,不支持基元。

It is not possible.

If you annotate your method with @objc you will see the problem.

Throwing method cannot be marked @objc because it returns a value of type 'Int'; return 'Void' or a type that bridges to an Objective-C class

You can return only objects, primitives are not supported.

更多推荐

本文发布于:2023-07-28 12:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1305185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   工作   Objective   Swift   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!