// MARK: - 字典转模型
/**
通过字典来创建一个模型
- parameter dict: 字典
- returns: 模型对象
*/
public class func sj_objectWithKeyValues(dict: NSDictionary) -> AnyObject? { }
/**
通过plist来创建一个模型,不需要后缀
- parameter fileName: 文件名(仅限于mainBundle中的文件)
- returns: 模型对象
*/
public class func sj_objectWithFilename(fileName:String) -> AnyObject? { }
/**
通过plist来创建一个模型
- parameter file: 文件全路径
- returns: 模型对象
*/
public class func sj_objectWithFile(file:String) -> AnyObject? { }
// MARK: - 字典数组转模型数组
/**
通过字典数组来创建一个模型数组
- parameter dictArray: 字典数组
- returns: 模型数组
*/
public class func sj_objectArrayWithKeyValuesArray(dictArray: NSArray) -> NSArray? { }
/**
通过plist来创建一个模型数组,不需要后缀
- parameter fileNmae: 文件名(仅限于mainBundle中的文件)
- returns: 模型数组
*/
public class func sj_objectArrayWithFilename(fileNmae:String) -> NSArray? { }
/**
通过plist来创建一个模型数组
- parameter file: 文件全路径
- returns: 模型数组
*/
public class func sj_objectArrayWithFile(file:String) -> NSArray? { }
/**
自定义关系映射
- returns: 返回可选映射关系字典 [属性名: 自定义对象类型]
*/
optional static func sj_customeClassMapping() -> [String: String]?
/**
属性名替换
- returns: 返回替换列表[属性名 : 替换名]
*/
optional static func sj_setupReplacedKeyFromPropertyName() -> [String: String]?
import UIKit
class SJTopicItem: NSObject {
// 间距
private let maring:CGFloat = 10
// 最热评论标题高度
private let topcmtH:CGFloat = 20
/** 用户的名字 */
var name:String?
/** 用户的头像 */
var profile_image:String?
/** 帖子的文字内容 */
var text:String?
/** 帖子审核通过的时间 */
var created_at:String?
/** 顶数量 */
var ding:Int32 = 0
/** 踩数量 */
var cai:Int32 = 0
/** 转发\分享数量 */
var repost:Int32 = 0
/** 评论数量 */
var comment:Int32 = 0
/** 帖子的类型 */
var type:Int = 0
/** 最热评论 */
var top_cmt:[SJComment]?
/** 图片的真实宽度 */
var width:NSString?
/** 图片的真实高度 */
var height:NSString?
/** 小图 */
var small_image:String?
/** 中图 */
var middle_image:String?
/** 大图 */
var large_image:String?
/** 音频时长 */
var voicetime:Int = 0
/** 视频时长 */
var videotime:Int = 0
/** 音频\视频的播放次数 */
var playcount:Int = 0
/** 是否是GIF图片 服务器返回 0 1 */
var is_gif:Bool?
/***** 额外增加的属性 - 方便开发 *****/
/** 中间内容的frame */
var contentF:CGRect?
/** 是否为超长图片 */
var bigPicture:Bool?
var t:NSNumber?
static func sj_customeClassMapping() -> [String : String]? {
return ["top_cmt" : "\(SJComment.classForCoder())"]
}
static func sj_setupReplacedKeyFromPropertyName() -> [String : String]? {
return ["small_image" : "image0", "large_image" : "image1", "middle_image" : "image2"]
}
// 目前Bool需要手动处理
override func setValue(value: AnyObject?, forKey key: String) {
if key == "is_gif" {
if (value as! String) == "1" {
is_gif = true
return
}
is_gif = false
return
}
super.setValue(value, forKey: key)
}
}
let path = NSBundle.mainBundle().pathForResource("testDict.plist", ofType: nil)
let dictArray = NSArray(contentsOfFile: path!)
// print(dictArray)
let dict = SJTopicItem.sj_objectWithFile(path!) as! SJTopicItem
print(dict.profile_image!)
let items = SJTopicItem.sj_objectArrayWithFilename("testArr")
let item = items![0] as! SJTopicItem
let comment = item.top_cmt![0]
let user = comment.user
print(user!.username!)
print(user!.is_vip!)
// json字符串转模型
func jsonToModel() {
let jsonStrin = "{\"name\":\"king\", \"age\":20, \"height\":165, \"tmp\":null}"
let user = SJJsonStringModel.sj_objectWithJsonString(jsonStrin) as? SJJsonStringModel
print("JSON字符串字典")
print(user?.name)
print(user?.age)
print(user?.height)
print(user?.tmp)
print("JSON字符串字典--end")
let jsonArr = "[{\"name\":\"king\", \"age\":20, \"height\":165, \"tmp\":null},{\"name\":\"jom\", \"age\":18, \"height\":160, \"tmp\":null},{\"name\":\"fuck\", \"age\":10, \"height\":100, \"tmp\":null}]"
let userArr = SJJsonStringModel.sj_objectWithJsonString(jsonArr) as? [SJJsonStringModel]
print("JSON字符串数组字典")
for var item in userArr! {
print(item.name)
print(item.age)
print(item.height)
print(item.tmp)
}
}
JSON字符串字典
Optional("king")
Optional(20)
Optional(165.0)
Optional(<null>)
JSON字符串字典--end
JSON字符串数组字典
Optional("king")
20
165.0
Optional(<null>)
Optional("jom")
18
160.0
Optional(<null>)
Optional("fuck")
10
100.0
Optional(<null>)
CocoaPods 安装
platform :ios, '8.0'
use_frameworks!
target '你项目的target' do
pod 'SJExtension'
end
improt SJExtension
手动集成
将SJExtension文件夹拖入你的工程即可