我熟悉在Android中使用AsyncTask:创建子类,在子类的实例上调用execute,在UI线程或主线程上调用onPostExecute.什么是swift中的等价物?
我有一个(大量)数据,我想用alamofire和siftyJSON发送到Table View
网页请求:
let postEndPoint :String = "http://jsonplaceholder.typicode.com/posts/"
Run Code Online (Sandbox Code Playgroud)
代码Alamofire和SwiftyJSON:
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, postEndPoint).responseJSON { response in
// handle json
guard response.result.error == nil else{
print("error calling Get on /posts/1")
print(response.result.error)
return
}
if let value: AnyObject = response.result.value{
let post = JSON(value)
// for i in 0...post.count{
if let title = post["data"].arrayValue as? [JSON]{
self.datas = title
self.tableView.reloadData()
print("the title is :\(title)" )
}else{
print("eror parsing /post/1 ")
}
// }
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码表视图:
func tableView(tableView: …Run Code Online (Sandbox Code Playgroud) 我经历过类似的问题,但仍然不明白为什么我的代码会抛出错误.
let posts = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as?[String: AnyObject]
print(posts)
for post in posts! {
var postObject:Post?
if let id = post["ID"] as? Int , let name = post["Country_Name"]as? String{ //Type '(String, AnyObject)' has no subscript members
postObject = Post(ID: id, Name: name)
self.CountryId.append(id)
}
self.CountrySelected.append(postObject!)
Run Code Online (Sandbox Code Playgroud)
json数据:
[
{
"$id": "1",
"ID": 1,
"Country_Name": "sample string 2"
},
{
"$ref": "1"
}
]
Run Code Online (Sandbox Code Playgroud)
当我使用[[[String:AnyObject]]时
错误: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to …