二 Swift網路請求回來的資料我這樣取

2021-09-11 12:25:40 字數 3024 閱讀 8156

原始碼位址 swift 2.3 alamofire3.0版本支援 ios 8 swift 3 alamofire4.0以上版本支援 ios 9及以上系統

json資料:

,,,

,);},,

,,,);

},,,,

,);});

pic = (,,

);};

msg = "\u6210\u529f";

ret = 1;

}複製**

模型:

import uikit

class basemodel: nsobject

//重寫父類的方法

override func setvalue(_ value: any?, forundefinedkey key: string)

}class typessa***: basemodel

typessatdata = typessatdata(dict: tempdata)}}

var typessatdata: typessatdata?

}class typessatdata: basemodel

for dict in tempcate_list }}

var pic: [[string: nsobject]]?

for dict in banner_list }}

var cates: [typessatcate] = [typessatcate]()

var pics:[typessatpic] = [typessatpic]() // 輪播圖陣列

}class typessatcate: basemodel

for dict in tempsublist }}

var sublists: [typessatsublist] = [typessatsublist]()

}class typessatpic: basemodel

class typessatsublist: basemodel

複製**

使用 eg:

let

type: typessa*** = typessa***(dict: (result as? [string : nsobject])!)

print(type.typessatdata?.pics.first?.picurl ?? "")

複製**

原始碼: 初始化:

let json = json(data: datafromnetworking)

let json = json(jsonobject)

if let datafromstring = jsonstring.data(using: .utf8, allowlossyconversion: false)

一些常用的方法:

//從 json陣列 中獲取乙個 double

let name = json[0].double

//用來顯示 json 中是否有指定的值

if json["name"].exist()

複製**

required init?(map: map)

}複製**

模型:

import uikit

var ret: int = 0

var data: typedata?

var msg: string = ""

required init?(map: map)

ret <- map["ret"]

data <- map["data"]

msg <- map["msg"]

}} var pic: [typepic] = [typepic]()

var cate: [typecate] = [typecate]()

required init?(map: map)

pic <- map["pic"]

cate <- map["cate"]

}} var picurl: string = ""

var prodid: int = 0

var text1: string = ""

var price: int = 0

var text2: string = ""

var storeid: int = 0

var link: string = ""

var type: int = 0

required init?(map: map)

picurl <- map["picurl"]

}} var picurl: string = ""

var id: int = 0

var sublist: [typesublist] = [typesublist]()

var grade: int = 0

var name: string = ""

var parid: string = ""

required init?(map: map)

name <- map["name"]

}} var parid: int = 0

var id: int = 0

var grade: int = 0

var name: string = ""

var picurl: string = ""

required init?(map: map)

name <- map["name"]

}}複製**

使用eg:

let

print(test1?.data?.cate.first?.name ?? "")

複製**

swift 同步 網路請求 swift網路請求

swift同樣可以實現oc中afnetworking mjextension的效果,實現方法是alamofire swiftyjson handyjson alamofire 網路請求 swiftyjson 資料解析 handyjson 對映為model 一 獲取sessionmanager子類的單...

Swift網路請求

這裡以json資料格式為例介紹 常見資料格式json xml等 下面例子使用nsurlconnection實現了乙個簡單的非同步get操作 func requesturl urlstring string else 這個庫通過封裝nsurlconnection和nsoperationqueue實現了...

swift 網路請求

1 alamofire swift 網路請求庫 2 使用 1 引入 檔名 httpprotocol 2 宣告請求的變數 var request httprequest 3 post 傳字串請求 let bodystr nsstring format page 1 rows 100000 lx 0 n...