0

I am working with a MapKit example using swift. The JSON object they are using is a object of arrays. I have a JSON file that is a array of objects. from the example I see they are pulling the properties they want by there location in the array. I need to use the property keys in my objects. How do I do this? Very first time with swift. thanks

here are example files

 init(title: String, locationName: String, discipline: String, coordinate: CLLocationCoordinate2D) {
self.title = title
self.locationName = locationName
self.discipline = discipline
self.coordinate = coordinate

  super.init()
 }

    class func fromJSON(json: [JSONValue]) -> Artwork? {
// 1
var title: String
if let titleOrNil = json[16].string {
  title = titleOrNil
} else {
  title = ""
}
let locationName = json[12].string
let discipline = json[15].string

  // 2
  let latitude = (json[18].string! as NSString).doubleValue
  let longitude = (json[19].string! as NSString).doubleValue
  let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

   // 3
    return Artwork(title: title, locationName: locationName!, discipline: discipline!, coordinate: coordinate)
  }

  var subtitle: String {
   return locationName
 }

  // MARK: - MapKit related methods

  // pinColor for disciplines: Sculpture, Plaque, Mural, Monument, other
  func pinColor() -> MKPinAnnotationColor  {
   switch discipline {
  case "Sculpture", "Plaque":
   return .Red
 case "Mural", "Monument":
   return .Purple
 default:
   return .Green
 }
  }

 // annotation callout opens this mapItem in Maps app
func mapItem() -> MKMapItem {
let addressDict = [String(kABPersonAddressStreetKey): self.subtitle]
let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDict)

let mapItem = MKMapItem(placemark: placemark)
mapItem.name = self.title

  return mapItem
}

Snippet of JSON file

  [  
     198,
     "9E5E1F28-22AF-459A-841E-5E89B022505E",
     198,
     1340413921,
     "436621",
     1340413921,
     "436621",
     "{\n}",
     null,
     null,
     "1933",
     "Bronze plaque mounted on a stone with an inscription marking the site of an artesian well. Located along Wilder Avenue near Artesian Way.",
     "1922 Wilder Avenue",
     "http://hiculturearts.pastperfect-online.com/34250images/004/193301-2.JPG",
     "1933.01",
     "Plaque",
     "Site of Honolulu's Pioneer Artesian Well",
     "Full",
     "21.30006",
     "-157.827969",
     [  
        null,
        "21.30006",
        "-157.827969",
        null,
        false
     ],
     null
  ],

snippet of JSON file i want to use

{
            "id": "301B2",
            "name": "Wrenhurst",
            "lat": "35.815864",
            "lng": "-78.918893",
            "status": "Act 2Q12",
            "minp": "632000",
            "maxp": "678000",
            "annStarts": "14",
            "annClosings": "0",
            "bldList": "Builder example",
            "vdl": "0",
            "futures": "0",
            "lotSize": "95'",

1 Answer 1

1

It looks like your example is using SwiftyJSON You can look at the docs to get more information on how to get data out of a JSONValue object, but specifically, if I understand your question, you need to know how to get values using keys rather than position.

OK, it depends on what values you are getting and whether or not those values can be null in your JSON file (aka nil in Swift)

I'm going to quote the example that you gave and then show you how to get that value from your JSON file

If you know for a fact that a value will never be null in your JSON file and will always be provided:

// The code from your example using array position
let locationName = json[12].string

// Would convert to:
let locationName = json["name"].string

// And in the case of lat / lng
let latitude = (json[18].string! as NSString).doubleValue

// This converts to:
let latitude = (json["lat"].string! as NSString).doubleValue

Simply use the key instead of the position to get at your value. However, this is not the only change you need. Instead of passing an array of JSONValue objects to your factory method, you need to specify a normal JSONValue (or just a JSON object)

So your factory method changes from this:

class func fromJSON(json: [JSONValue]) -> Artwork? {
    // 1
    var title: String
    if let titleOrNil = json[16].string {
        title = titleOrNil
    } else {
        title = ""
    }
    let locationName = json[12].string
    let discipline = json[15].string

    // 2
    let latitude = (json[18].string! as NSString).doubleValue
    let longitude = (json[19].string! as NSString).doubleValue
    let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

    // 3
   return Artwork(title: title, locationName: locationName!, discipline: discipline!, coordinate: coordinate)
}

To something like this:

// This class method should return an Artwork object or nil if one couldn't be created for any reason. 
// In your example, I couldn't find any reason you would return nil, so... in this example, it simply returns and Artwork object
class func fromJSON(json: JSON) -> Artwork {
    // 1
    var title: String
    if let titleOrNil = json["title"].string {
        title = titleOrNil
    } else {
        title = ""
    }
    let locationName = json["name"].string
    let discipline = json["discipline"].string

    // 2
    let latitude = (json["lat"].string! as NSString).doubleValue
    let longitude = (json["lng"].string! as NSString).doubleValue
    let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

    // 3
   return Artwork(title: title, locationName: locationName!, discipline: discipline!, coordinate: coordinate)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.