Define an array:
var a:[string]=["File", "Edit", "View", "Navigate"]
Define a dictionary:
var airports: [String:string] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
Defines a nsmutabledictionary array
vara:[string]=["File","Edit","View","Navigate"]varD:nsmutabledictionary = ["Xcode":"name","6.1.1":"version","QQ"A
Convert a dictionary to JSON data:
var jsondata = Nsjsonserialization.datawithjsonobject (dictionary, Options:nil, Error:nil)
Convert JSON to Dictionary data:
as Nsdictionary
JSON interacts with the server for data
1. Use Nodejs to create a simple Web service that spits out JSON data:
var http=require(‘http‘) var port = ‘8081‘ var data = {‘name‘: ‘jifeng‘, ‘company‘: ‘taobao‘};
http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type‘: ‘text/html; charset=UTF-8‘,‘Server‘:‘NodeJs‘});
res.end(JSON.stringify(data));
}).listen(port, "127.0.0.1");
2. Request data with Swift native class:
var url = NSURL(string: "http://localhost:8081") var request = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:{
(response, data, error) -> Void in if (error? != nil) { //Handle Error here print(error)
}else{
let jsonData = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as NSDictionary
println(jsonData) //Handle data in NSData type }
})
Swift Basic Knowledge Finishing