我目前正在尝试将映射工具应用到我的模型中,目前我做这件事的方式并没有产生任何结果。我有一个MediaLibrary类,它包含2个数组,' exerciseMedia'和' stretchMedia'他们都拥有一系列媒体'对象。我选择 ObjectMapper / AlamofireObjectMapper 库将数据映射到我的模型。
import ObjectMapper
class MediaLibrary: Mappable {
var exerciseMedia: [Media]?
var stretchMedia: [Media]?
required init?(map: Map) {
mapping(map: map)
}
func mapping(map: Map) {
exerciseMedia <- map["ExerciseMedia"]
stretchMedia <- map["StretchMedia"]
}
}
这是Media对象,它将构成填充MediaLibrary类中两个数组的对象。
import ObjectMapper
struct Media: Mappable {
var _id: String?
var name: String?
var desc: String?
var imageURI: String?
var videoURI: String?
var trainerID: String?
var isShoulders : Bool?
var isNeck : Bool?
var isLegs : Bool?
var isHands : Bool?
var isChest : Bool?
var isCalves : Bool?
var isButt : Bool?
var isBack : Bool?
var isArms : Bool?
var isCore : Bool?
var isFullBody : Bool?
var isLowerBody : Bool?
var isUpperBody : Bool?
var isExercise : Bool?
var isStretch : Bool?
var equipmentNeeded : String?
init?(map: Map){ }
mutating func mapping(map: Map) {
_id <- map["_id"]
name <- map["name"]
desc <- map["desc"]
imageURI <- map["imageURI"]
videoURI <- map["videoURI"]
trainerID <- map["trainerID"]
isShoulders <- map["isShoulders"]
isNeck <- map["isNeck"]
isLegs <- map["isLegs"]
isHands <- map["isHands"]
isChest <- map["isChest"]
isCalves <- map["isCalves"]
isButt <- map["isButt"]
isBack <- map["isBack"]
isArms <- map["isArms"]
isCore <- map["isCore"]
isFullBody <- map["isFullBody"]
isLowerBody <- map["isLowerBody"]
isUpperBody <- map["isUpperBody"]
isExercise <- map["isExercise"]
isStretch <- map["isStretch"]
equipmentNeeded <- map["equipmentNeeded"]
}
}
现在这里是我想要映射的json的样本。请注意,我的模型有&#39; imageURI&#39;和&#39; videoURI&#39;因为有2种类型的媒体对象,但由于它们是可选项,因此它可以解释这一点。
{
"StretchMedia": [
{
"_id": "5a380db7ee292409b4f44980",
"name": "somephoto",
"desc": "some description",
"imageURI": "https://videofitapptestbucket.s3.us-west-2.amazonaws.com/100006166323",
"trainerID": "5a32f62a28ce4acbc5fbbb4b",
"__v": 0,
"createdAt": "2017-12-18T18:49:27.009Z",
"isShoulders": false,
"isNeck": false,
"isLegs": false,
"isHands": false,
"isChest": false,
"isCalves": false,
"isButt": false,
"isBack": false,
"isArms": false,
"isCore": false,
"isFullBody": false,
"isLowerBody": false,
"isUpperBody": false,
"isExercise": false,
"isStretch": true,
"equipmentNeeded": "none"
},
{
"_id": "5a380dfa089b2d09dcfa92db",
"name": "somephoto",
"desc": "some description",
"videoURI": "https://videofitapptestbucket.s3.us-west-2.amazonaws.com/100002098677",
"trainerID": "5a32f62a28ce4acbc5fbbb4b",
"__v": 0,
"createdAt": "2017-12-18T18:50:34.277Z",
"isShoulders": false,
"isNeck": false,
"isLegs": false,
"isHands": false,
"isChest": false,
"isCalves": false,
"isButt": false,
"isBack": false,
"isArms": false,
"isCore": false,
"isFullBody": false,
"isLowerBody": false,
"isUpperBody": false,
"isExercise": false,
"isStretch": true,
"equipmentNeeded": "none"
}
],
"ExerciseMedia": [
{
"_id": "5a345a356ec5fc0032651611",
"name": "Juan",
"desc": "pic of some Spanish guy",
"imageURI": "https://videofitapptestbucket.s3.us-west-2.amazonaws.com/10000998839",
"trainerID": "5a32f62a28ce4acbc5fbbb4b",
"isStretchCore": false,
"isExerciseCore": false,
"__v": 0,
"createdAt": "2017-12-15T23:26:45.461Z",
"isShoulders": false,
"isNeck": false,
"isLegs": false,
"isHands": false,
"isChest": false,
"isCalves": false,
"isButt": false,
"isBack": false,
"isArms": false,
"isCore": false,
"isFullBody": true,
"isLowerBody": false,
"isUpperBody": false,
"isExercise": true,
"isStretch": false,
"equipmentNeeded": "Dumbbells"
}
],
}
我遇到的问题是如何使用&#34; ExerciseMedia&#34;的内容填充我的MediaLibrary类上的2个数组。和&#34; StretchMedia&#34;他们各自的阵列。我创建了一个函数,它向包含您刚刚看到的JSON的路径发出请求(正文只需要教练ID)。我到目前为止发布了我的代码。
import AlamofireObjectMapper
var mediaLibrary: MediaLibrary?
func setTrainerMediaLibrary(trainerID: String) {
let body: [String: Any] = [
"trainerID": trainerID
]
Alamofire.request(TRAINER_MEDIA_ALL, method: .post, parameters: body, encoding: JSONEncoding.default, headers: HEADER).responseObject { (response: DataResponse<MediaLibrary>) in
if response.result.error == nil {
self.mediaLibrary = Mapper<MediaLibrary>().map(JSONObject: response.result.value)
} else {
print("Could not map JSON data to Media Library model")
debugPrint(response.result.error as Any)
}
}
}
MediaLibrary对象返回nil,所以事情并不正确。我的想法是,因为MediaLibrary已经映射了&#34; ExerciseMedia&#34;和&#34; StretchMedia&#34;我需要做的就是在请求中使用MediaLibrary,映射函数将自动处理您在JSON中看到的2个媒体键。我可能会对此完全错误,但我知道我很接近,任何帮助都会受到赞赏。
答案 0 :(得分:1)
至于AlamofireObjectMapper的用法,对象响应已经映射到json。您只需将其称为.value
即可使用。
self.mediaLibrary = response.result.value
Mapper<MediaLibrary>().map()
的用法是指ObjectMapper库本身。