Swift框架的嵌套类在Objective C中无法很好地导出 [英] Swift framework nested classes are not exported well in Objective C

查看:64
本文介绍了Swift框架的嵌套类在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:

import geography

func setFields()
{
    var contactName:String = Contact.Fields.Name
    var countryPrefix:String = Country.Fields.Prefix
    var contactsTable: String  = Country.Table
}

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.

public class Country: NSObject {

    @objc(CountryFields) public class Fields: NSObject {
        // ...
    }
}

This should expose your Switf's 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.

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屋!

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