gpt4 book ai didi

ios - 使用 swift 发起 HTTP GET 请求

转载 作者:行者123 更新时间:2023-11-29 01:39:33 26 4
gpt4 key购买 nike

我正在尝试快速获取请求并填充我的 TableView 。截至目前,我已经准备好需要填充到我的 table 中的 json 数据,看起来像 enter image description here

因为我刚刚开始学习swift,所以我发现提出请求有点困惑。这是我的 RestaurantTableViewController.swift

override func viewDidLoad() {
super.viewDidLoad()

var url: String = "http://localhost:8888/restaurant/registeruser.php"

var request: NSMutableURLRequest = NSMutableURLRequest()

request.URL = NSURL(string: url)

request.HTTPMethod = "GET"

request.timeoutInterval = 60


NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (respose:NSURLResponse!, data:NSData!, error:NSError!) -> Void in

var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil

let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary

if (jsonResult != nil){
//success
println(jsonResult)

let dataArray = jsonResult["name"] as! NSArray;

for item in dataArray { //loop through data items

let obj = item as! NSDictionary

for(key, value) in obj{

let allnames = obj["name"] as! NSString;
}
}
}
}

}

这就是我到目前为止所做的一切,在此之后,我应该如何让我的填充餐厅名称类型,和 位置。任何彻底的帮助将不胜感激。

最佳答案

我建议使用 https://github.com/daltoniam/JSONJoy-Swift它会让事情变得更容易和更清洁。

至于通话

var DISPATCH_TIME_FOREVER: UInt64 { get }

var url: String = "http://localhost:8888/restaurant/registeruser.php"
let sem = dispatch_semaphore_create(0)

var responseData = NSData()
NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: {(data, response, error) -> Void in
responseData = data
dispatch_semaphore_signal(sem)
}).resume()
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER)
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary

if (jsonResult != nil){
//success
println(jsonResult)

let dataArray = jsonResult["name"] as! NSArray;

for item in dataArray { //loop through data items

let obj = item as! NSDictionary

for(key, value) in obj{

let allnames = obj["name"] as! NSString;
}
}
}

关于ios - 使用 swift 发起 HTTP GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32566535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com