How to use Swift non-NSObject subclass in Objective-C

前端 未结 1 1728
抹茶落季
抹茶落季 2021-01-26 13:21

I\'m experiencing some issues to use Swift in my Objective-C project.

I use the following lib for example (https://github.com/Hearst-DD/ObjectMapper). I\'ve tried the th

1条回答
  •  -上瘾入骨i
    2021-01-26 13:52

    In Objective-C you cannot create a class without a superclass. Thats why the @objc will not work without at least NSObject.

    So you have to use at least NSObject. Thats how i would do it

    @objc class SSStreetAddressRequest: NSObject {
        var street:String?
        var city:String?
        var state:String?
        var zip:String?
    }
    
    extension SSStreetAddressRequest : Mappable {
        required init?(_ map: Map){
    
        }
    
        @objc func mapping(map: Map) {
            street  <- map["street"]
            city    <- map["city"]
            state   <- map["state"]
            zip     <- map["zip"]
        }
    }
    

    0 讨论(0)
提交回复
热议问题