1

I am trying to import a Swift 3 class in ObjC but I am not able to do it for some reasons. The class is not a UIViewController class and the solutions which I found is perfectly working for View Controller classes but not for Model class. Can anyone help?

open class UserDetails: JSONEncodable {
    public var id: Int64?
    public var usertoken: String?
    public var firstName: String?
    public var lastName: String?
    public var email: String?
    .....
}
Nishant Bhindi
  • 2,242
  • 8
  • 21
Khushal Dugar
  • 230
  • 4
  • 12

1 Answers1

1

For a Swift class to be visible in Objective-C, it has to be a NSObject subclass. Also, the types have to be Objective-C compatible (e.g. optional numeric types like Int64? can't be represented in Objective-C). It has to be non-optional or make it a NSNumber.

See Writing Swift Classes and Protocols with Objective-C Behavior in Using Swift with Cocoa and Objective-C.


FYI, I know you asked about Swift 3, but if writing Swift 4, make sure to use @objcMembers:

@objcMembers
open class UserDetails: NSObject, JSONEncodable {
    public var id: Int64 = 0
    public var usertoken: String?
    public var firstName: String?
    public var lastName: String?
    public var email: String?
    ...
}

Or if only some of these need to be available from Objective-C, just designate the individual properties/methods with @objc qualifier.

Rob
  • 415,655
  • 72
  • 787
  • 1,044