Fast structure nested classes are not exported to Objective-C

I have the following classes contained in Geography.framework (Swift framework project):

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" } } 

In my quick application using this structure, everything runs 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 the Contact and Country classes, but the nested Fields classes are not visible. Also, the value of Contact.Table is not displayed.

What do I need to do to have the same library structure and library usage in ObjectiveC?

Thanks,

+6
source share
2 answers

Here you should be explicit with the definition for ObjC.

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

This should be shown on your Country.Fields switch for your ObjC as CountryFields . I have not tested it, but I believe that you do not need to explicitly specify inheritance with NSObject . The @objc attribute should do this for you when compiling.

Update for Swift 3:

This seems to have been broken in Swift 3 and will not be fixed. https://bugs.swift.org/browse/SR-2267?focusedCommentId=21033&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21033

0
source

You can use the typealias trick to have the same syntax when using it:

 public class Contact : NSObject { public static let Table: String = "contacts" typealias Fields = ContactFields } @objcMembers public class ContactFields : NSObject { public static let Id: String = "_id" public static let Name: String = "name" static let rawId: String = "rawId" } public class Country : NSObject { typealias Fields = CountryFields } @objcMembers public class CountryFields : NSObject { public static let Id: String = "_id" public static let Prefix: String = "prefix" static let rawId: String = "rawId" } 
0
source

All Articles