Swift框架的嵌套类在Objective C中无法很好地导出 [英] Swift framework nested classes are not exported well in Objective C
问题描述
我有Geography.framework(一个Swift框架项目)包含的以下类:
public class Contact : NSObject
{
public static let Table: String = "contacts"
public class Fields : NSObject
{
public static let Id: String = "_id"
public static let Name: String = "name"
static let rawId: String = "rawId"
}
}
public class Country : NSObject
{
public class Fields : NSObject
{
public static let Id: String = "_id"
public static let Prefix: String = "prefix"
static let rawId: String = "rawId"
}
}
在使用此框架的快速应用程序中,所有操作均顺利进行:
import geography
func setFields()
{
var contactName:String = Contact.Fields.Name
var countryPrefix:String = Country.Fields.Prefix
var contactsTable: String = Country.Table
}
好吧,如果我在ObjectiveC中使用相同的Geography.framework,我会看到Contact和Country类,但是看不到嵌套的类Fields.另外,看不到Contact.Table的值.
要在ObjectiveC中具有相同的库结构和库用法,我需要做什么?
谢谢
您必须在此处明确定义ObjC.
public class Country: NSObject {
@objc(CountryFields) public class Fields: NSObject {
// ...
}
}
这应该将ObjC的Switf的Country.Fields
公开为CountryFields
.我还没有测试过,但是我相信您不必明确地从NSObject
继承. @objc
属性应在编译时为您完成.
Swift 3的更新:
看起来像这样在Swift 3中已损坏,无法修复. In my swift app using this framework everything works smoothly: Well, if I use the same Geography.framework in ObjectiveC, I see Contact and Country class but the nested classes Fields are not seen. Also the value of Contact.Table is not seen. What I need to do in order to have same library structure and library usage in ObjectiveC? Thank you, You have to be explicit here with definition for ObjC. This should expose your Switf's Update for Swift 3: Looks like this was broken in Swift 3 and won't be fixed. https://bugs.swift.org/browse/SR-2267?focusedCommentId=21033&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21033 这篇关于Swift框架的嵌套类在Objective C中无法很好地导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!import geography
func setFields()
{
var contactName:String = Contact.Fields.Name
var countryPrefix:String = Country.Fields.Prefix
var contactsTable: String = Country.Table
}
public class Country: NSObject {
@objc(CountryFields) public class Fields: NSObject {
// ...
}
}
Country.Fields
for your ObjC as CountryFields
. I haven't tested it but I believe you don't have to be explicit about inheriting from NSObject
. @objc
attribute should do it for you when compiling.