ObjectMapper的使用举例2 ThemeRecipe.swift

import UIKit

import ObjectMapper


struct ThemeRecipeList :Mappable{

    // 组类型的themeRecipes进行一个实例化

    var themeRecipes:[ThemeRecipe] = [ThemeRecipe]()

    //每种theme都是ThemeRecipe组的类型

    var theme1:[ThemeRecipe]?{

        didSet{

            guard let theme1 = theme1 else{

                return

            }

            themeRecipes += theme1

        }

    }

    var theme2:[ThemeRecipe]?{

        didSet{

            guard let theme2 = theme2 else{

                return

            }

            themeRecipes += theme2

        }

    }

    var theme3:[ThemeRecipe]?{

        didSet{

            guard let theme3 = theme3 else{

                return

            }

            themeRecipes += theme3

        }

    }

    var theme4:[ThemeRecipe]?{

        didSet{

            guard let theme4 = theme4 else{

                return

            }

            themeRecipes += theme4

        }

    }

    var theme5:[ThemeRecipe]?{

        didSet{

            guard let theme5 = theme5 else{

                return

            }

            themeRecipes += theme5

        }

    }

    var theme6:[ThemeRecipe]?{

        didSet{

            guard let theme6 = theme6 else{

                return

            }

            themeRecipes += theme6

        }

    }

    var theme7:[ThemeRecipe]?{

        didSet{

            guard let theme7 = theme7 else{

                return

            }

            themeRecipes += theme7

        }

    }

    var theme8:[ThemeRecipe]?{

        didSet{

            guard let theme8 = theme8 else{

                return

            }

            themeRecipes +=  theme8

        }

    }

    var headerRecipes:[ThemeRecipe]?

    

    init?(map: Map) {

        

    }


    mutating func 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!

    }

    

    mutating func 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) {

        

    }

    

    mutating func 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{

            guard let recipeList = recipeList else{

                return

            }

     //这里左边的themeRecipes是要被赋值的,使用sorted方法进行排序,将themeRecipes里的所有的对象进行排序后一个一个的赋值给themeRecipes

  //sorted的作用只是将里面的对象进行 排序

            self.themeRecipes = recipeList.themeRecipes.sorted { (theme1, theme2) -> Bool in

                return theme1.locationId < theme2.locationId

            }

       //右边的headerRecipes是模型解析中的 headerRecipes,然后赋值给了左边的headerRecipes

            self.headerRecipes = recipeList.headerRecipes

        

        }

    }

    //此处有了排序后的themeRecipes。

    var themeRecipes:[ThemeRecipe]?{

        didSet{

            collectionView?.reloadData()

        }

    }

    

    var headerRecipes:[ThemeRecipe]?{

        didSet{

            guard let headerRecipes = headerRecipes else{

                return

            }

     //这里的headerRecipes得到上面的赋值后,给了对应的模型

            cycleView.imageURLStringsGroup = headerRecipes.map{$0.image_url!}

        }

    }

    

//

 override func numberOfSections(in collectionView: UICollectionView) -> Int {

        return 1

    }


  //得到值后的 themeRecipes 就可以这样使用了

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return themeRecipes?.count ?? 0

    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值