在prepareForSegue中与swift_dynamicCast链接的崩溃

时间:2014-06-26 09:29:13

标签: ios swift cs193p

首先,这是我的代码:

let flickerFetcher : FlickrFetcher = FlickrFetcher()

var photos : UIImage[]? = UIImage[]() {
    didSet { self.appsTableView.reloadData() }
}

func prepareImageViewController(ivc: ImageViewController, toDisplayPhoto photo: NSDictionary)
{
    ivc.imageURL = flickerFetcher.URLforPhoto(photo, format: FlickrFetcher.FlickrPhotoFormat.Large)
    ivc.title = photo.valueForKeyPath(flickerFetcher.FLICKR_PHOTO_TITLE) as String
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
 //   NSLog("Prepare for Segue beginning")
    if sender!.isKindOfClass(UITableViewCell) {
      //  NSLog("Sender is UITableViewCell")
        var indexPath : NSIndexPath = self.tableView.indexPathForCell(sender as UITableViewCell)
        if indexPath != nil {
          //  NSLog("IndexPath is nil")
            if segue!.identifier == "Display Photo"{
               // NSLog("Segue Identified")
                if segue!.destinationViewController.isKindOfClass(ImageViewController) {
                    NSLog("DestinationViewController is ImageViewController")
                    var destinationController = segue.destinationViewController as ImageViewController
                    if let photoDict = self.photos?[indexPath.row] as? NSDictionary {
                        self.prepareImageViewController(destinationController, toDisplayPhoto:photoDict)
                    }

                }
            }
        }
    }

}

我已经将问题缩小到最后一行的prepareImageViewController的实现,这使我崩溃链接到swift_dynamicCast。我似乎无法弄明白。

1 个答案:

答案 0 :(得分:2)

在使用之前,请尝试并确保您的照片字典。

if let photoDict = self.photos?[indexPath.row] as? NSDictionary {
    self.prepareImageViewController(destinationController, toDisplayPhoto:photoDict)
}