ObjectMapper的使用举例2 ThemeRecipe.swift
来源:互联网 发布:电脑拍摄软件 编辑:程序博客网 时间:2024/05/16 07:36
import UIKit
import ObjectMapper
struct ThemeRecipeList :Mappable{
// 数组类型的themeRecipes进行一个实例化
var themeRecipes:[ThemeRecipe] = [ThemeRecipe]()
//每种theme都是ThemeRecipe数组的类型
var theme1:[ThemeRecipe]?{
didSet{
guardlet theme1 = theme1else{
return
}
themeRecipes += theme1
}
}
var theme2:[ThemeRecipe]?{
didSet{
guardlet theme2 = theme2else{
return
}
themeRecipes += theme2
}
}
var theme3:[ThemeRecipe]?{
didSet{
guardlet theme3 = theme3else{
return
}
themeRecipes += theme3
}
}
var theme4:[ThemeRecipe]?{
didSet{
guardlet theme4 = theme4else{
return
}
themeRecipes += theme4
}
}
var theme5:[ThemeRecipe]?{
didSet{
guardlet theme5 = theme5else{
return
}
themeRecipes += theme5
}
}
var theme6:[ThemeRecipe]?{
didSet{
guardlet theme6 = theme6else{
return
}
themeRecipes += theme6
}
}
var theme7:[ThemeRecipe]?{
didSet{
guardlet theme7 = theme7else{
return
}
themeRecipes += theme7
}
}
var theme8:[ThemeRecipe]?{
didSet{
guardlet theme8 = theme8else{
return
}
themeRecipes += theme8
}
}
var headerRecipes:[ThemeRecipe]?
init?(map:Map) {
}
mutatingfunc mapping(map: Map) {
headerRecipes<- map["-1"]
theme1<- map["1"]
theme2<- map["2"]
theme3<- map["3"]
theme4<- map["4"]
theme5<- map["5"]
theme6<- map["6"]
theme7<- map["7"]
theme8<- map["8"]
}
}
struct ThemeRecipeArray: Mappable {
var obj :[ThemeRecipe]?
var key ="1"
init?(map:Map) {
key = map.JSON.keys.first!
}
mutatingfunc mapping(map: Map) {
obj<- map[key]
}
}
struct ThemeRecipe: Mappable {
var description:String?
var favorite:Bool?
var locationName:String?
var recipe_type:String?
var click_count:Int?
var favorite_count:Int?
var image_url:String?
var rid:Int?
var title:String?
var locationId:Int?
var recommend_type:String?
var recipe_id:Int?
var group_id:String?
var share_count:String?
var str_date:String?
init?(map:Map) {
}
mutatingfunc mapping(map: Map) {
description<- map["description"]
favorite<- map["favorite"]
locationName<- map["locationName"]
recipe_type<- map["recipe_type"]
click_count<- map["click_count"]
favorite_count<- map["favorite_count"]
image_url<- map["image_url"]
rid<- map["rid"]
title<- map["title"]
locationId<- map["locationId"]
recommend_type<- map["recommend_type"]
recipe_id<- map["recipe_id"]
group_id<- map["group_id"]
share_count<- map["share_count"]
str_date<- map["str_date"]
}
}
//////////////////////////////////////////////////////////////////////////////////
解析好的对象进行赋值的操作:
在DiscoverViewController.swift中:
var recipeList:ThemeRecipeList?{
didSet{
guardlet recipeList = recipeList else{
return
}
//这里左边的themeRecipes是要被赋值的,使用sorted方法进行排序,将themeRecipes里的所有的对象进行排序后一个一个的赋值给themeRecipes
//sorted的作用只是将里面的对象进行 排序
self.themeRecipes = recipeList.themeRecipes.sorted { (theme1, theme2) -> Boolin
return theme1.locationId< theme2.locationId
}
//右边的headerRecipes是模型解析中的 headerRecipes,然后赋值给了左边的headerRecipes
self.headerRecipes = recipeList.headerRecipes
}
}
//此处有了排序后的themeRecipes。
var themeRecipes:[ThemeRecipe]?{
didSet{
collectionView?.reloadData()
}
}
var headerRecipes:[ThemeRecipe]?{
didSet{
guardlet headerRecipes = headerRecipes else{
return
}
//这里的headerRecipes得到上面的赋值后,给了对应的模型
cycleView.imageURLStringsGroup = headerRecipes.map{$0.image_url!}
}
}
overridefunc numberOfSections(in collectionView:UICollectionView) -> Int {
return1
}
//得到值后的 themeRecipes 就可以这样使用了
overridefunc collectionView(_ collectionView:UICollectionView, numberOfItemsInSection section:Int) -> Int {
returnthemeRecipes?.count ??0
}
- ObjectMapper的使用举例2 ThemeRecipe.swift
- ObjectMapper的使用举例 Recipe.swift
- 【Swift】代理使用举例
- iOS JSON与Object之间的映射 ObjectMapper For Swift
- 使用jackson的ObjectMapper json转object
- Jackson的ObjectMapper类简单使用
- spring jack 使用自定义的objectMapper。或者在xml配置objectMapper参数
- spring jack 使用自定义的objectMapper。或者在xml配置objectMapper参数
- ObjectMapper 简单使用
- 1ObjectMapper、SimpleModule、JsonNode三个类解释 2ObjectMapper类的writeValueAsString、valueToTree、readTree等方法
- swift举例
- ObjectMapper 的使用和常用注解 过滤条件
- 系统性学习Moya+Alamofire+RxSwift+ObjectMapper的配合使用
- 摘抄的ObjectMapper用法
- ObjectMapper 的一个坑
- objectmapper的转化问题
- ObjectMapper JSON的 处理
- ObjectMapper
- 基础练习 FJ的字符串
- [随笔]利用云虚拟机和学校VPN实现校外访问校内站点(反向代理)
- 下一个大计算平台? Amazon Echo 研发幕后全揭露
- C语言 蛇形填数
- 小A点菜
- ObjectMapper的使用举例2 ThemeRecipe.swift
- [随笔]利用云虚拟机和学校VPN实现校外访问校内站点(反向代理)_0
- 二叉树的深度(二叉树)
- underscore.js学习
- 42、判断是否以元音字母结尾
- 遍历所有驱动的名_根据本驱动对象的成员(DriverObject->DriverSection)_对应LDR_DATA_TABLE_ENTRY结构体_双向链表使用
- JAVA高精度4_递推求值
- 《游戏编程模式》(6)
- macOS Sierra 10.12.2与intellij 2017 EAP与Docker 1.13.0