gpt4 book ai didi

json - swift ,对象映射器 : Type 'User' does not conform to protocol 'Mappable'

转载 作者:行者123 更新时间:2023-11-30 14:02:27 25 4
gpt4 key购买 nike

我对 Swift 还很陌生,所以请耐心等待。我正在尝试使用 ObjectMapper 将 Swift 对象转换为 JSON 字符串。

我尝试将我的对象与 Mappable 接口(interface)一起使用,但我不断收到此错误。

类型“ProductDTO”不符合协议(protocol)“Mappable”

我从 git 文档中复制粘贴了确切的代码,但出现了相同的错误。请帮忙,我已经花了 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'

我完全不知道是什么原因造成的。

我尝试使用 Cocoapods、Carthage 重新安装 ObjectMapper,并最终将其安装为子模块,但没有成功。请帮忙!谢谢!

最佳答案

我刚刚使用最新版本的 ObjectMapper 构建了代码,没有错误。 ObjectMapper 几天前更改了协议(protocol),根据最新版本,您的代码看起来很好,只需更新到 ObjectMapper 即可,它应该可以正常工作。

最新版本是0.17

关于json - swift ,对象映射器 : Type 'User' does not conform to protocol 'Mappable' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com