无法转换类型的值 - 自定义点注释

时间:2016-01-18 14:37:25

标签: ios swift mapkit mkpointannotation

在我的MapViewController上,我能够成功地从CloudKit下载所有记录,并将它们作为引脚放置在它们的位置(标题和副标题)。

当我点击它时,我尝试为每个引脚创建缩略图图像。我收到错误消息:

无法将'Foodie.CustomPointAnnotation'(0x7f85dff41b00)类型的值转换为'Foodie.Place'(0x1077b5fa0)。

这是viewForAnnotation方法:

  func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
  {
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(Constants.PinIdentifier)

    if pinView == nil
    {
      pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: Constants.PinIdentifier)
      pinView?.canShowCallout = true
    }

    pinView!.annotation = annotation
    (annotation as! Place).loadMapImage { image in
      if image != nil
      {
        UIGraphicsBeginImageContext(CGSize(width: 30, height: 30))
        image.drawInRect(CGRectMake(0, 0, 30, 30))
        let smallImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        let imView = UIImageView(image: smallImage)
        pinView?.leftCalloutAccessoryView = imView
      }
    }

    let customPointAnnotation = annotation as! CustomPointAnnotation
    pinView!.image = UIImage(named: customPointAnnotation.pinCustomImageName)

    return pinView
  }

这是我的模特(地点):

import UIKit
import CloudKit
import MapKit

let placeName = "name"
let placeAddress = "address"
let placeComment = "comment"
let placePhoto = "photo"
let placeRating = "rating"
let placeLocation = "location"

class Place
{
  var name: String
  var address: String
  var comment: String?
  var photo: UIImage?
  var rating: Int
  var location: CLLocation?
  var identifier: String
  var record: CKRecord!

  init(record: CKRecord)
  {
    self.record = record
    self.name = record.valueForKey(placeName) as! String
    self.address = record.valueForKey(placeAddress) as! String
    self.comment = record.valueForKey(placeComment) as? String

    if let photoAsset = record.valueForKey(placePhoto) as? CKAsset
    {
      self.photo = UIImage(data: NSData(contentsOfURL: photoAsset.fileURL)!)
    }

    self.rating = record.valueForKey(placeRating) as! Int
    self.location = record.valueForKey(placeLocation) as? CLLocation

    self.identifier = record.recordID.recordName
  }

  // MARK: Map Annotation

  var coordinate: CLLocationCoordinate2D? {
    get {
      return location!.coordinate
    }
  }

  var title: String! {
    get {
      return name
    }
  }

  var subtitle: String! {
    get {
      return address
    }
  }

  func loadMapImage(completion: (image: UIImage!) -> ())
  {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0))
    {
      var image: UIImage!
      if let mapImage = self.record.objectForKey(placePhoto) as? CKAsset
      {
        image = UIImage(data: NSData(contentsOfURL: mapImage.fileURL)!)
      }
      completion(image: image)
    }
  }

}

知道我做错了什么吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

要使强制转换工作,注释实际上必须是Place对象或Place的子类。没有PlacePlace的某些内容的自动翻译。

最好的解决方案可能是为Place创建一个以CustomPointAnnotation作为参数的初始化程序。