Try searching the database at https://opendata.socrata.com? that’s where I found the Honolulu public art data—I think all Socrata data sets provide JSON downloads.
It looks like there have been significant changes to Swift syntax since this tutorial was posted. I am having particular trouble with the loadInitialData function that uses the NSJSONSerialization to parse JSON data into Artwork Objects. Can anyone tell me what changes have been made since this was posted that would prevent the function from working properly? Thanks
Now I am getting the message “unable to read data” on the let filename = … line in the debugger. Is there something more I need to do to make the file JSON.swift discoverable within the scope of the program?
When you have the PublicArt.json file, drag it into your HonoluluArt\Supporting Files group, make sure Destination: Copy items if needed and Add to targets: HonoluluArt are selected
and/or check that HonoluluArt is checked in Target Membership:
Thanks again, Audrey! I simply forgot to call the method loadInitialData() and mapView.addAnnotations(artwork). Although I did confirm all the other suggestions before I figured out what I did wrong. Everything worked great!
I did try to parse a JSON file from the Seattle data portal at data.seattle.gov thinking that I could just change the name of the file and the coordinates of the initial location. When I tried to run it I got a giant list of fatal errors.
I am not sure if this didn’t work because the JSON file varied slightly from the Honolulu Art dataset, or if it’s because I didn’t make the proper changes to the Swift code.
I downloaded that file—it’s Socrata, but Socrata allows some flexibility in the ordering of the data items. My fromJSON method is tailored to the Honolulu public art JSON file. If you look at the start of the Seattle file, before the actual data, you’ll see "name" : "Common Name", is at "position" : 3, but you’ll need to add 7 to position to get the correct data array index—look at a data item, and count within the array. I notice the Seattle file puts longitude before latitude, the opposite to the Honolulu file, so take care of that when modifying fromJSON.
If you want the app to download the JSON file from the url, you’ll need to use NSData(contentsOfURL:options:)
I was wondering how you would implement dropping a pin at the user’s current location? Let’s say they wanted to permanently save their current location. Is it possible to do this with a simple button? Thanks for all the help.
I would like to know how come this static func fromObject(object: AnyObject) → JSONValue? {
switch object {
case let value as NSString:
return JSONValue.JSONString(value as String)
case let value as NSNumber:
return JSONValue.JSONNumber(value)
case let value as NSNull:
return JSONValue.JSONNull
case let value as NSDictionary:
var jsonObject: [String:JSONValue] = [:]
for (k: AnyObject, v: AnyObject) in value {
if let k = k as? NSString {
if let v = JSONValue.fromObject(v) {
jsonObject[k as String] = v
} else {
return nil
}
}
}
return JSONValue.JSONObject(jsonObject)
case let value as NSArray:
var jsonArray: [JSONValue] = []
for v in value {
if let v = JSONValue.fromObject(v) {
jsonArray.append(v)
} else {
return nil
}
}
return JSONValue.JSONArray(jsonArray)
default:
return nil
}
}
in JSON.swift is giving me an error Definition conflicts with previous value. Thank you;
sorry, I don’t get this error when I open the final version of this project. Do a search in Xcode on fromObject, to see if you have another definition somewhere?
Thanks a lot. That fixed it. If its not a bother, would it be possible to ask some more regarding a different topic like how to locate beacons accurately in swift? Thanks a lot.