ObjectMapper的使用举例 Recipe.swift
2017-01-27 13:09
435 查看
在Recipe.swift的模型文件中,有如下内容:
import UIKit
import ObjectMapper
//json解析
//为了支持映射,类或者构造体只需要实现Mappable协议,
struct RecipeList: Mappable
{
//声明
var code:String?
var msg:String?
//一般变量
var data :[Recipe]?
// Recipe的数组
//协议包含的第一个方法
init?(map:
Map) {
}
//协议包含的第一个方法,ObjectMapper使用自定义的<-
运算符来声明成员变量和 JSON
的映射关系。
mutating
func mapping(map:
Map) {
//映射关系,没有加入Recipe的数组
data
<- map["data"]
code
<- map["code"]
msg
<- map["msg"]
}
}
//大多数情况下你都可以使用框架提供的转换类 TransformOf
来快速的实现一个期望的转换。
//这里有一个transfromOfTupleAndString()的方法,供下面的调用,方法的类型是TransformOf<(CGFloat,CGFloat,CGFloat),String>,是一种泛型。
func transfromOfTupleAndString()->TransformOf<(CGFloat,CGFloat,CGFloat),String>{
// TransformOf 的初始化需要两个类型和两个闭包。两个类型声明了转换的目标类型和源类型,闭包则实现具体转换逻辑。
//(CGFloat,CGFloat,CGFloat)是screeningId的类型
return
TransformOf<(CGFloat,CGFloat,CGFloat),String>.init(fromJSON:
{ (screenID) -> (CGFloat,
CGFloat, CGFloat)?
in
// 把值从 String?
转成 (CGFloat,CGFloat,CGFloat)?
let defaultV:CGFloat =
3
//screenID 就是要转换的String,也就是screeningId
if
let string = screenID
{
let strArr = string.components(separatedBy:
",")
if strArr.count >=
3
{
let value1 = (strArr[0]
as NSString).floatValue
let value2 = (strArr[1]
as NSString).floatValue
let value3 = (strArr[2]
as NSString).floatValue
return (CGFloat(value1),CGFloat(value2),CGFloat(value3))
}
}
return (defaultV,defaultV,defaultV)
}, toJSON: { (value) -> String?
in
// 把值从 (CGFloat,CGFloat,CGFloat)?
转成 String?
let (value1,value2,value3) = value!
return
"\(value1),\(value2),\(value3)"
})
}
struct Recipe: Mappable {
//Recipe的数组里的变量声明
var detailsUrl:String?
var clickCount:Int?
var id:Int?
var categoryID:Int?
var description:String?
var releaseDate:String?
var type:String?
var screeningId:(CGFloat,CGFloat,CGFloat)?
var maketime:String?
var name:String?
var shareCount:Int?
var createDate:TimeInterval?
var modifyDate:TimeInterval?
var imageUrl:String?
var title:String?
init?(map:
Map) {
}
mutating
func mapping(map:
Map) {
//Recipe的数组的映射关系
detailsUrl
<- map["detailsUrl"]
clickCount
<- map["clickCount"]
id
<- map["id"]
categoryID
<- map["categoryID"]
description
<- map["description"]
releaseDate
<- map["releaseDate"]
type
<- map["type"]
maketime
<- map["maketime"]
name
<- map["name"]
shareCount
<- map["shareCount"]
createDate
<- map["createDate"]
modifyDate
<- map["modifyDate"]
imageUrl
<- map["imageUrl"]
title
<- map["title"]
//这里的transfromOfTupleAndString()
进行自定义转换规则处理的调用
screeningId
<- (map["screeningId"],transfromOfTupleAndString())
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
在 RecipeViewController中使用这个模型文件:
先创建相应的实例,
var recipeList = [Recipe]()
/////////////////////////////////////////
// MARK: - 网络请求
func loadNewData()
{
currentpage =
0
recipeList.removeAll()
loadMoreData()
}
这里在第一次请求数据的时候会将以前的recipeList 对象进行清空。
recipeList对象在这下面有使用:
extension RecipeViewController:UICollectionViewDelegate,UICollectionViewDataSource
{
func collectionView(_ collectionView:
UICollectionView, numberOfItemsInSection section:
Int) -> Int {
return
recipeList.count
}
func collectionView(_ collectionView:
UICollectionView, cellForItemAt indexPath:
IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
ArticleCellID, for: indexPath)
as! ArticleCell
return cell
}
func collectionView(_ collectionView:
UICollectionView, willDisplay cell:
UICollectionViewCell, forItemAt indexPath:
IndexPath) {
guard
let cell = cell as?
ArticleCell else{
return
}
cell.recipeData =
recipeList[(indexPath
as NSIndexPath).item]
}
func collectionView(_ collectionView:
UICollectionView, didEndDisplaying cell:
UICollectionViewCell, forItemAt indexPath:
IndexPath) {
guard
let cell = collectionView.cellForItem(at: indexPath)
as? ArticleCell
else{
return
}
cell.foodImageView.image =
nil
}
func collectionView(_ collectionView:
UICollectionView, didSelectItemAt indexPath:
IndexPath) {
if
let id = recipeList[(indexPath
as NSIndexPath).item].id {
performSegue(withIdentifier:
"showDetail", sender: id)
}
}
}
////////////////////////////////////////////////////////////////////////////
import UIKit
import ObjectMapper
//json解析
//为了支持映射,类或者构造体只需要实现Mappable协议,
struct RecipeList: Mappable
{
//声明
var code:String?
var msg:String?
//一般变量
var data :[Recipe]?
// Recipe的数组
//协议包含的第一个方法
init?(map:
Map) {
}
//协议包含的第一个方法,ObjectMapper使用自定义的<-
运算符来声明成员变量和 JSON
的映射关系。
mutating
func mapping(map:
Map) {
//映射关系,没有加入Recipe的数组
data
<- map["data"]
code
<- map["code"]
msg
<- map["msg"]
}
}
//大多数情况下你都可以使用框架提供的转换类 TransformOf
来快速的实现一个期望的转换。
//这里有一个transfromOfTupleAndString()的方法,供下面的调用,方法的类型是TransformOf<(CGFloat,CGFloat,CGFloat),String>,是一种泛型。
func transfromOfTupleAndString()->TransformOf<(CGFloat,CGFloat,CGFloat),String>{
// TransformOf 的初始化需要两个类型和两个闭包。两个类型声明了转换的目标类型和源类型,闭包则实现具体转换逻辑。
//(CGFloat,CGFloat,CGFloat)是screeningId的类型
return
TransformOf<(CGFloat,CGFloat,CGFloat),String>.init(fromJSON:
{ (screenID) -> (CGFloat,
CGFloat, CGFloat)?
in
// 把值从 String?
转成 (CGFloat,CGFloat,CGFloat)?
let defaultV:CGFloat =
3
//screenID 就是要转换的String,也就是screeningId
if
let string = screenID
{
let strArr = string.components(separatedBy:
",")
if strArr.count >=
3
{
let value1 = (strArr[0]
as NSString).floatValue
let value2 = (strArr[1]
as NSString).floatValue
let value3 = (strArr[2]
as NSString).floatValue
return (CGFloat(value1),CGFloat(value2),CGFloat(value3))
}
}
return (defaultV,defaultV,defaultV)
}, toJSON: { (value) -> String?
in
// 把值从 (CGFloat,CGFloat,CGFloat)?
转成 String?
let (value1,value2,value3) = value!
return
"\(value1),\(value2),\(value3)"
})
}
struct Recipe: Mappable {
//Recipe的数组里的变量声明
var detailsUrl:String?
var clickCount:Int?
var id:Int?
var categoryID:Int?
var description:String?
var releaseDate:String?
var type:String?
var screeningId:(CGFloat,CGFloat,CGFloat)?
var maketime:String?
var name:String?
var shareCount:Int?
var createDate:TimeInterval?
var modifyDate:TimeInterval?
var imageUrl:String?
var title:String?
init?(map:
Map) {
}
mutating
func mapping(map:
Map) {
//Recipe的数组的映射关系
detailsUrl
<- map["detailsUrl"]
clickCount
<- map["clickCount"]
id
<- map["id"]
categoryID
<- map["categoryID"]
description
<- map["description"]
releaseDate
<- map["releaseDate"]
type
<- map["type"]
maketime
<- map["maketime"]
name
<- map["name"]
shareCount
<- map["shareCount"]
createDate
<- map["createDate"]
modifyDate
<- map["modifyDate"]
imageUrl
<- map["imageUrl"]
title
<- map["title"]
//这里的transfromOfTupleAndString()
进行自定义转换规则处理的调用
screeningId
<- (map["screeningId"],transfromOfTupleAndString())
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
在 RecipeViewController中使用这个模型文件:
先创建相应的实例,
var recipeList = [Recipe]()
/////////////////////////////////////////
// MARK: - 网络请求
func loadNewData()
{
currentpage =
0
recipeList.removeAll()
loadMoreData()
}
这里在第一次请求数据的时候会将以前的recipeList 对象进行清空。
recipeList对象在这下面有使用:
extension RecipeViewController:UICollectionViewDelegate,UICollectionViewDataSource
{
func collectionView(_ collectionView:
UICollectionView, numberOfItemsInSection section:
Int) -> Int {
return
recipeList.count
}
func collectionView(_ collectionView:
UICollectionView, cellForItemAt indexPath:
IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
ArticleCellID, for: indexPath)
as! ArticleCell
return cell
}
func collectionView(_ collectionView:
UICollectionView, willDisplay cell:
UICollectionViewCell, forItemAt indexPath:
IndexPath) {
guard
let cell = cell as?
ArticleCell else{
return
}
cell.recipeData =
recipeList[(indexPath
as NSIndexPath).item]
}
func collectionView(_ collectionView:
UICollectionView, didEndDisplaying cell:
UICollectionViewCell, forItemAt indexPath:
IndexPath) {
guard
let cell = collectionView.cellForItem(at: indexPath)
as? ArticleCell
else{
return
}
cell.foodImageView.image =
nil
}
func collectionView(_ collectionView:
UICollectionView, didSelectItemAt indexPath:
IndexPath) {
if
let id = recipeList[(indexPath
as NSIndexPath).item].id {
performSegue(withIdentifier:
"showDetail", sender: id)
}
}
}
////////////////////////////////////////////////////////////////////////////
相关文章推荐
- ObjectMapper的使用举例2 ThemeRecipe.swift
- object和embed使用及举例
- Object-c KVC的使用和举例
- ObjectMapper 的使用和常用注解 过滤条件
- Swift 3 使用objc_setAssociatedObject
- Jackson(ObjectMapper)的简单使用(可转xml)
- object和embed使用及举例
- json字符串出现转义符,导致调用者使用ObjectMapper解析成Map报错
- swift Alamofire+ObjectMapper——swift(学习九)
- [快速学会Swift第三方库]AlamofireObjectMapper
- 简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)
- 使用ObjectMapper把json转换为实体-时间转换问题
- [Jackson] 使用ObjectMapper对含有任意key的JSON进行反序列化
- Android进步之十四:使用jackon的ObjectMapper的writeValueAsString方法可以把java对象转化成json字符串
- Swift: Alamofire -> http请求 & ObjectMapper -> 解析JSON
- iOS使用Quartzcode设计动画,快速得到Object-C 和 Swift 代码。
- object和embed使用及举例
- 现在是放弃 Object-C 使用 Swift 的最好时机
- 在xcode 6.4 中使用swift和object-c混合编程,同时通过cocoapods进行管理的一些问题和解决办法
- AlamofireObjectMapper框架的基本使用以及二次封装