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

    }




0 0
原创粉丝点击