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?
    .....
}
0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot but for swift3 when I just made UserDetails subclass of NSObject and added @ObjC it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.