简体   繁体   English

Swift,ObjectMapper:类型“用户”不符合协议“可映射”

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

I am very new to Swift, so please bear with me. 我是Swift的新手,请多多包涵。 I am trying to use ObjectMapper to convert my Swift object to a JSON String. 我正在尝试使用ObjectMapper将我的Swift对象转换为JSON字符串。

I tried using my object with the Mappable interface but I keep getting this error. 我尝试将对象与Mappable接口配合使用,但始终收到此错误。

Type 'ProductDTO' does not conform to protocol 'Mappable' 类型“ ProductDTO”不符合协议“适用”

I copied pasted the exact code from the git documentation but I get the same error. 我从git文档中复制并粘贴了确切的代码,但出现了同样的错误。 Please help, I have spent a good 3 hours on this. 请帮助,我已经花了3个小时了。

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. 我尝试使用Cocoapods,Carthage重新安装ObjectMapper,最后将其安装为子模块,但是没有运气。 Please help! 请帮忙! thanks! 谢谢!

I just built the code with latest version of ObjectMapper and there were no errors. 我只是用最新版本的ObjectMapper构建了代码,没有任何错误。 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. 几天前,ObjectMapper更改了协议,根据最新版本,您的代码看起来还不错,只需将ObjectMapper更新到,它就可以正常工作。

Latest release is 0.17 最新版本是0.17

I Googled my issues and realised that probably my Xcode version is old. 我用Google搜索了问题,并意识到我的Xcode版本可能很旧。 I updated it to version 7.0.1 and everything's working great! 我将其更新为版本7.0.1,一切正常! Thank you everyone! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“字符串”不符合协议“ NSCopying”-数组swift json错误 - Type 'String' does not conform to protocol 'NSCopying' - Array swift json Error Swift 4.2:类型“ T”不符合协议“可解码” - Swift 4.2 : Type 'T' does not conform to protocol 'Decodable' 类型“”不符合协议“可编码” - Type ' ' does not conform to protocol 'Encodable' Swift ObjectMapper:toJSONString用于包含可映射对象的字典 - Swift ObjectMapper: toJSONString for a dictonary containing mappable Object 如果我在 swift 的结构中使用协议类型,我的结构不符合协议“可解码”/“可编码” - My structure does not conform to protocol 'Decodable' / 'Encodable' if I use protocol type in my structure in swift Argo:类型不符合“可解码”协议 - Argo: Type does not conform to protocol 'Decodable' 类型[String:String]不符合协议“ AnyObject” - type [String: String] does not conform to protocol 'AnyObject' 类型“ AreaData”不符合协议“可编码” - Type 'AreaData' does not conform to protocol 'Encodable' SwiftUI-类型“服务”不符合协议“可解码” - SwiftUI - Type 'Service' does not conform to protocol 'Decodable' 尝试在Swift中使用SwiftyJSON循环时,JSON类型不符合协议SecuenceType - Type JSON does not conform to protocol SecuenceType when trying to loop using SwiftyJSON in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM