我对快速的语言更新鲜。任何人都可以解释如何致电服务&存储在nsmutableArray或nsmutableDictionary中,就像我们在Objective-c语言中一样。
答案 0 :(得分:1)
试试这个示例代码
声明数组
var textArr = [String]()
var nameArr = [String]()
在swift3中使用alamofire
Alamofire.request(url, parameters: ["user":"any username"
,"media_id":"anyid"]).responseJSON { response in
if response.result.isSuccess == true
{
if let value = response.result.value {
let json = JSON(value)
print(self.json)
let comment_arr = self.json["comment_arr"]
for (index, _): (String, JSON) in comment_arr {
let i : Int = Int(index)!
let name = comment_arr[i]["user"].stringValue
let text = comment_arr[i]["text"].stringValue
self.nameArr.append(name)
self.textArr.append(text)
}
self.tableView.reloadData()
self.loadingNotification.hide(animated: true)
}
}
else
{
}
}
我希望它对你有用!!!
答案 1 :(得分:1)
Swift 2.0
使用API检索帖子,解析帖子,存储在数组中并使用Almofire
更新UI。
Alamofire.request(.GET, "http://jsonplaceholder.typicode.com/posts", parameters: nil)
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.result) // result of response serialization
let json = JSON(data: response.data!)
print(json[0]["title"])
switch response.result
{
case .Success:
let jsonResponse = JSON(data: response.data!)
if jsonResponse.count > 0
{
self.arrPosts.addObjectsFromArray(jsonResponse.arrayObject!)
}
dispatch_async(dispatch_get_main_queue())
{
// *** Update your UI/UX here ***
}
case .Failure:
print(response.debugDescription)
}
}