我确信这对你们所有经验丰富的人来说都是愚蠢的,但对初学者来说这很难!
我基本上用一个显示json列表的url填充表。
所以我把它放在最上面:
super.viewDidLoad()
get_data_from_url("http://www.example.com/my_jsonfile.php")
以下代码适用于没有参数的简单网址:
func get_data_from_url(url:String)
{
let httpMethod = "GET"
let timeout = 15
let url = NSURL(string: url)
let urlRequest = NSMutableURLRequest(URL: url!,
cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 15.0)
let queue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(
urlRequest,
queue: queue,
completionHandler: {(response: NSURLResponse!,
data: NSData!,
error: NSError!) in
if data.length > 0 && error == nil{
let json = NSString(data: data, encoding: NSASCIIStringEncoding)
self.extract_json(json!)
}else if data.length == 0 && error == nil{
println("Nothing was downloaded")
} else if error != nil{
println("Error happened = \(error)")
}
}
)
}
但是如何在那里获取我的参数?即:网址应为:
http://www.example.com/my_jsonfile.php?the_date=2015-05-16&leaving=yes
答案 0 :(得分:1)
您必须连接URL字符串,以便所需的参数包含在其中:
var url = "http://www.example.com/my_jsonFile.php?"
var parameters = ["the_date=2015-05-16", "leaving=yes"]
for parameter in parameters{
url+=parameter
if parameter != parameters.last{
url+="&"
}
}