简体   繁体   中英

Swift, ObjectMapper: Type 'User' does not conform to protocol 'Mappable'

I am very new to Swift, so please bear with me. I am trying to use ObjectMapper to convert my Swift object to a JSON String.

I tried using my object with the Mappable interface but I keep getting this error.

Type 'ProductDTO' does not conform to protocol 'Mappable'

I copied pasted the exact code from the git documentation but I get the same error. Please help, I have spent a good 3 hours on this.

import Foundation
import ObjectMapper

class User: Mappable {
   var username: String?
   var age: Int?
   var weight: Double!
   var array: [AnyObject]?
   var dictionary: [String : AnyObject] = [:]
   var bestFriend: User?                       // Nested User object
   var friends: [User]?                        // Array of Users
   var birthday: NSDate?

   required init?(_ map: Map){

   }

   // Mappable
   func mapping(map: Map) {
     username    <- map["username"]
     age         <- map["age"]
     weight      <- map["weight"]
     array       <- map["arr"]
     dictionary  <- map["dict"]
     bestFriend  <- map["best_friend"]
     friends     <- map["friends"]
     birthday    <- (map["birthday"], DateTransform())
   }
}

Error: Type 'User' does not conform to protocol 'Mappable'

I have absolutely no idea what could be causing this.

I tried reinstalling ObjectMapper using Cocoapods, Carthage and finally installed it as a submodule, but no luck. Please help! thanks!

I just built the code with latest version of ObjectMapper and there were no errors. ObjectMapper had changed the protocol few days ago and you code just seems fine according to latest release, just update to ObjectMapper to and it should work fine.

Latest release is 0.17

I Googled my issues and realised that probably my Xcode version is old. I updated it to version 7.0.1 and everything's working great! Thank you everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM