Search code examples
swiftannotationsmapkitplist

MapKit .PLIST DICTIONARY with INT and STRING-values = multiple annotations WITH Title & Subtitle from plist (according: MapKit Sample WWDC 2017 - 237)


I used the Project tandm from Apples WWDC 2017 - 237 "What's new in Mapkit".

There is already a data.plist with an array named bikes and additional dictionaries with four items (key : values -Pairs).

  1. item 0 = Dictionary-Nr (multiple Annotations from 0, 1, 2 ...)
  2. item = "key" lat for geo-data with "value" of data-type "number"
  3. item = "key" long for geo-data longitude as above
  4. type = "key" 0 (enumeration-defined in Class Bike) for tintColor and glyphImages for different annotations which are declared in bikeView.swift.

Following example works perfect:

data.plist shows:

  -> Root  - dictionary
  -> bikes - array
   -> item 0  - dictionary             (Annotation Nr. 1)
   -> lat     - number        50.12345 (latitude of Annotation Nr.1
   -> long    - number         6.12345 (longitude of Annot. Nr. 1)
   -> type    - number         1       (two different 0 or 1)
 

Here is the code which reads the dictionaries item 0, item 1...:

// Class bike.swift 

import MapKit 

class Bike: MKPointAnnotation {

enum BikeType: Int {
    case unicycle
    case bicycle 
}
    var type: BikeType = .tricycle 
func bikes(fromDictionaries dictionaries: [[String: NSNumber]]) -> [Museum] {

let bikes = dictionaries.map { item -> Bike in
let bike = Bike()

bike.coordinate = CLLocationCoordinate2DMake(item["lat"]!.doubleValue, item["long"]!.doubleValue)

bike.type = BikeType(rawValue: item["type"]!.intValue)!

    return bike
   }
  return bike
 }
}

I want to implement a fifth and sixth (key) named "title" and "subtitle" (with String-values each) to the Dictionary of the data.plist (named item 0), for example:

  1. title = "foo"
  2. subtitle = "bar"

Solution

  • Add the following property to bike.swift

    var title: String?
    var subtitle: String?
    

    Implement a fifth and sixth (key) named "title" and "subtitle" (with String-values each) to the data.plist

    enter image description here

    Run Tandm.project

    enter image description here