title: Swift CoreData: Unable to cast annotation.view as Pin
body: <p>I'm working on the CoreData project on Udacity and am having some difficulty casting annotation.view as Pin. The purpose of casting it as Pin would allow me to push Pin into the next VC to retrieve its respective photos via CoreData filtered by predicate. My codes as such:</p>
  
  <pre><code>// Pin Model
  import Foundation
  import CoreData
  import MapKit
  
  public class Pin: NSManagedObject, MKAnnotation {
  
  convenience init(lat: Double, long: Double, context: NSManagedObjectContext) {
  
      if let ent = NSEntityDescription.entity(forEntityName: "Pin", in: context) {
          self.init(entity: ent, insertInto: context)
          self.lat = lat
          self.long = long
      } else {
          fatalError("Unable to find Entity name!")
      }
  }
  
  public var coordinate: CLLocationCoordinate2D {
      get {
          return CLLocationCoordinate2D(latitude: lat as Double, longitude: long as Double)
      }
  
      set {
          lat = newValue.latitude
          long = newValue.longitude
      }
  }
  }
  
  //MapViewController
   func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -&gt; MKAnnotationView? {
      let reuseId = "pin"
  
      let pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView ?? MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
  
      pinView.animatesDrop = true
      pinView.isSelected = true
  
      return pinView
  }
  
  func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
      let pin = view.annotation as! Pin
      mapView.deselectAnnotation(pin, animated: false)
      viewPin(pin)
  }
  
  func viewPin(_ pin: Pin) {
      let controller = storyboard?.instantiateViewController(withIdentifier: "CollectionVC") as! PhotoAlbumCollectionViewController
      controller.pin = pin
      navigationController?.pushViewController(controller, animated: true)
  }
  </code></pre>
  
  <p>The error occurs at view.annotation as! Pin where it indicates that </p>
  
  <blockquote>
    <p>Could not cast value of type 'NSKVONotifying_MKPointAnnotation'
    (0x60800010d140) to 'VirtualTouristJR.Pin' (0x10f07b8b0).</p>
  </blockquote>
  
  <p>Could anyone pls advice, thanks!</p>
  
  <p>EDIT: Code for adding Pin</p>
  
  <pre><code>func addAnnotation(gestureRecognizer: UIGestureRecognizer) {
      if gestureRecognizer.state == UIGestureRecognizerState.began {
          let touchPoint = gestureRecognizer.location(in: mapView)
          let touchCoordinate = mapView.convert(touchPoint, toCoordinateFrom: mapView)
          let annotation = MKPointAnnotation()
          annotation.coordinate = touchCoordinate
  
          mapView.addAnnotation(annotation)
          print(touchCoordinate)
  
          let stack = delegate.stack
          addedPin = Pin(lat: touchCoordinate.latitude, long: touchCoordinate.longitude, context: (stack?.context)! )
          do {
              try stack?.saveContext()
          } catch {
              print("Error while saving.")
          }
      }
  }
  </code></pre>