【问题标题】:Could not cast value of type - Custom Point Annotation无法转换类型的值 - 自定义点注释
【发布时间】:2016-04-23 18:35:56
【问题描述】:

在我的 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)
    }
  }

}

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

【问题讨论】:

  • 发布您的班级。
  • 好的,将编辑我的初始帖子以包含所有内容。

标签: ios swift mapkit mkpointannotation


【解决方案1】:

要使强制转换起作用,注释实际上必须是Place 对象或Place 的子类。不是Place 的东西不会自动转换为Place

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

【讨论】:

  • 感谢您的建议。添加该初始化程序的最安全方法是什么?你指的是 viewForAnnotation 方法吗?我试图避免过多地改变模型,因为许多 VC 都依赖它。我还是 Swift/programming 的新手。
猜你喜欢
  • 1970-01-01
  • 2010-10-11
  • 2017-08-03
  • 2015-12-20
  • 2017-02-26
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多