My head is going to explode :) - I've been trying to get a JSON String from my server to a Dictionary Value, and I can't get it to work.
I'm trying to get (from my Server - this is dynamic and I want my app to be able to pull new data from the server when needed):
{"1":"Location 1","2":"Location 2","3":"Location 3"}
To this Dictionary in Xcode using Swift:
var labels = [
1 : "Location 1",
2 : "Location 2",
3 : "Location 3"
]
This has got to be pretty straight forward, but for the life of me I can't figure it out...
Here's my Swift - I can get it to pull the information from the server, but I can't get it into a dictionary like I need
var postEndpoint: String = "http://www.myserver.net/app/campus.php"
Alamofire.request(.GET, postEndpoint)
.responseJSON { (request, response, data, error) in
if let anError = error
{
println("error")
println(error)
}
else if let data: AnyObject = data
{
let post = JSON(data)
println(post)
}
}
which results in:
{
"1" : "Location 1",
"2" : "Location 2",
"3" : "Location 3"
}
The End Result that I'm using this for is an iBeacon implementation with the following code:
let knownBeacons = beacons.filter{ $0.proximity != CLProximity.Unknown }
if (knownBeacons.count > 0) {
let closestBeacon = knownBeacons[0] as CLBeacon
let locationID = post[closestBeacon.minor.integerValue]
self.locationLabel.text = locationID
self.view.backgroundColor = self.colors[closestBeacon.minor.integerValue]
}
The error I'm getting is at self.locationLabel.text = locationID 'JSON' is not convertible to 'String', I do not get this error when I use the static var labels dictionary. Am I trying to get the data from the server incorrectly? What am I doing wrong??? I think the var labels having an undeclared Type allows Swift to figure out what it needs to, how do I do the same from the JSON part?