如何在同一框架内访问Objective-C中的内部Swift类? [英] How to access an internal Swift class in Objective-C within the same framework?

查看:120
本文介绍了如何在同一框架内访问Objective-C中的内部Swift类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用混合框架。在Obj-C文件中导入,但内部类不可见,只有公共类。

Working on a mixed framework. imported inside the Obj-C file but the internal classes are not visible, only the public ones.

文档清楚地说明Swift和Obj-之间应该有内部类。 C:

The documentation clearly states the internal clasees should be available between Swift and Obj-C:


将Swift导入Objective-C
导入一组Swift文件框架目标作为您的Objective-C代码,您不需要将
导入到框架的伞头中。
相反,将Swift代码
的Xcode生成的头文件导入到您想要使用Swift代码的任何Objective-C .m文件中。
因为生成的框架目标标头是
框架的公共接口的一部分,所以只有标记为public
修饰符的声明才会出现在框架目标的生成标头中。
仍然可以使用在框架的Objective-C部分内用
内部修饰符标记的Swift方法和属性,
只要它们在继承自
Objective-C类
的类。有关访问级别修饰符的详细信息,请参阅
访问控制 doc / uid / TP40014097> Swift编程语言(Swift 2)。

Importing Swift into Objective-C
To import a set of Swift files in the same framework target as your Objective-C code, you don’t need to import anything into the umbrella header for the framework. Instead, import the Xcode-generated header file for your Swift code into any Objective-C .m file you want to use your Swift code from. Because the generated header for a framework target is part of the framework’s public interface, only declarations marked with the public modifier appear in the generated header for a framework target. You can still use Swift methods and properties that are marked with the internal modifier from within the Objective-C part of your framework, as long they are declared within a class that inherits from an Objective-C class. For more information on access-level modifiers, see Access Control in The Swift Programming Language (Swift 2).

代码示例(用框架创建一个新项目)

Code Sample (Create a new project with a framework)

// SwiftObject.swift

public class SwiftObject: NSObject {
    public class func doSomething() {}
}

internal class YetAnotherSwiftObject: NSObject {
    internal class func doSomething() {}
}







// SomeObject.m file

@implementation SomeObject

- (void)someMethod {
    [SwiftObject doSomething];
}

- (void)someOtherMethod {
    [YetAnotherSwiftObject doSomething]; // Use of undeclared identifier
}

@end


推荐答案

如文档中所示,标有 internal 修饰符的声明不会出现在生成的标题中,因此编译器不知道他们因而抱怨。当然,您可以使用 performSelector 方法发送消息,但这不方便且容易出错。我们只需要帮助编译器知道那些声明就在那里。

As indicated in the docs, declarations marked with internal modifier don't appear in the generated header, so the compiler does not know about them and thus complaints. Of course, you could send messages using performSelector approach, but that's not convenient and bug-prone. We just need to help the compiler know that those declarations are there.

首先,我们需要使用 @objc 属性变体,允许您在Objective-C中指定符号的名称:

First, we need to use @objc attribute variant that allows you to specify name for your symbol in Objective-C:

// SwiftObject.swift

@objc(SWIFTYetAnotherSwiftObject)
internal class YetAnotherSwiftObject: NSObject {
    internal class func doSomething() {}
}

然后你需要使用你想在代码中使用的方法创建 @interface 声明 - 所以编译器会很高兴,并且还使用您之前指定的符号名称应用 SWIFT_CLASS 宏 - 因此链接器将选择实际的实现:

And then you just need to create @interface declaration with the methods you want to use in your code - so the compiler will be happy, and also apply SWIFT_CLASS macro with the symbol name you've specified earlier - so the linker would pick the actual implementation:

// SomeObject.m file

SWIFT_CLASS("SWIFTYetAnotherSwiftObject")
@interface YetAnotherSwiftObject : NSObject

+ (void)doSomething;

@end


@implementation SomeObject

- (void)someOtherMethod {
    [YetAnotherSwiftObject doSomething]; // Should work now !!!
}

@end




  • 为了清楚起见,我在.m文件中使用了接口声明,更好的选择是在.h文件中组合这些声明,并包含它。

  • 通过声明方法接口我们正在向编译器做出承诺,如果你把一个不存在的方法(或者签名错误等)放在那里就不会抱怨。显然,在这种情况下你会在运行时崩溃 - 所以要小心。

  • 这篇关于如何在同一框架内访问Objective-C中的内部Swift类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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