加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift中使用Reactive获取网络数据,并字典转模型。

发布时间:2020-12-14 01:45:32 所属栏目:百科 来源:网络整理
导读:模型: // // Status.swift // ceshi // // Created by 胡双飞 on 15/10/11. // Copyright ? 2015年 HSF. All rights reserved. // import UIKit /// 微博模型 class Status: NSObject { //MARK:- 属性列表 /// 微博创建时间 var created_at: String? /// 微

模型:

//
// Status.swift
// ceshi
//
// Created by 胡双飞 on 15/10/11.
// Copyright ? 2015年 HSF. All rights reserved.
//

import UIKit
/// 微博模型
class Status: NSObject {


//MARK:- 属性列表

/// 微博创建时间
var created_at: String?

/// 微博ID
var id: Int = 0

/// 微博信息内容
var text:String?

/// 微博来源
var source:String?

//MARK:-字典转模型

init(dic: [String: AnyObject]) {
super.init()
setValuesForKeysWithDictionary(dic)
}

override func setValue(value: AnyObject?,forUndefinedKey key: String) {}

//对象的描述信息
override var description: String{

let keys = ["created_at","id","text","source"]

return dictionaryWithValuesForKeys(keys).description
}

}



// // StatusListViewModel.swift // WeiBo // // Created by 胡双飞 on 15/10/11. // Copyright ? 2015年 HSF. All rights reserved. // import UIKit import ReactiveCocoa /// 微博列表模型 - 分离网络加载 class StatusListViewModel: NSObject { //微博数据数组 var status:[AnyObject]? /// 加载微博数据 func loadStatus()-> RACSignal{ return RACSignal.createSignal({ (subscribe) -> RACDisposable! in NetworkTools.sharedTools.loadStatus().subscribeNext({ (result) -> Void in //1.获取网络数据,加载到字典数组中 guard let array = result["statuses"] as? [[String: AnyObject]] else{ return } //2.字典转模型 if self.status == nil{ //初始化Status模型的字典 self.status = [Status]() } //3.遍历模型 for dic in array{ self.status?.append(Status(dic: dic)) } subscribe.sendCompleted() },error: { (error) -> Void in subscribe.sendError(error) }) {} return nil }) } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读