SwiftUI学习记录 之 Record 3

219 阅读1分钟

struct 结构体

/*Codable:可编解码 Encodable:可编码  Decodable:可解码

 * enum CodingKeys:String,CodingKey{case:1} 映射key,可以处理冲突的key

*/

struct HomeModel:Decodable{

    var title : String

    var image = ""

    enum CodingKeys:String,CodingKey {

        case title

        case image = "imageName"

    }

}

json数据解析:

struct HomeList:Codable {

    var list:[HomeModel]

}

func loadDataWithJson(json:Data) -> HomeList {

    guard let list = try? JSONDecoder().decode(HomeList.self, from: json) else {

        fatalError("解析失败")

    }

    return list

}

闭包的使用:

/*第一个()中是需要回调的参数

 * 第二个()中是需要返回的数据

 void 为为参数或无返回值

*/

typealias SuccessBlock = ( _ resposeObj:Any?)->(Void)

typealias EventBlock = (T)->(Void)