线程 5:EXC_BAD_INSTRUCTION(代码 = EXC_I386_INVOP,子代码 = 0x0) - 从 'Result<AnyObject>' 转换为不相关的类型 '



我正在跟随Ray Wenderlich关于Alamofire(和挣扎)的教程,http://www.raywenderlich.com/85080/beginning-alamofire-tutorial#comments,我在标题中得到错误,在"加载更多照片"部分。

错误出现在"let photoinfo = ((JSON as!)NSDictionary) .valueForKey("照片")![NSDictionary])。过滤器({"

这是我的代码。

//
//  PhotoBrowserCollectionViewController.swift
//  Photomania
//
//  Created by Essan Parto on 2014-08-20.
//  Copyright (c) 2014 Essan Parto. All rights reserved.
//
import UIKit
import Alamofire
class PhotoBrowserCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
  var photos = NSMutableArray()
    @IBOutlet weak var PhotoBrowserCell: PhotoBrowserCollectionViewCell!
  let refreshControl = UIRefreshControl()
    var populatingPhotos = false
    var currentPage = 1
  let PhotoBrowserCellIdentifier = "PhotoBrowserCell"
  let PhotoBrowserFooterViewIdentifier = "PhotoBrowserFooterView"
  // MARK: Life-cycle
  override func viewDidLoad() {
    super.viewDidLoad()
    setupView()
    populatePhotos()
////    Alamofire.request(.GET, "https://api.500px.com/v1/photos").responseJSON() {
////        (_, _, data) in
////        print(data.value)}
//    
//    Alamofire.request(.GET, "https://api.500px.com/v1/photos", parameters: ["consumer_key": "Jj0vPllqD0zvxttgFZ1aTbRF5zy9g1yDcsTxJRFV"]).responseJSON() {
//        (_,_,JSON) in
//        print(JSON.value)
//        
//        let photoInfos = (JSON.value!.valueForKey("photos") as! [NSDictionary]).filter({
//            ($0["nsfw"] as! Bool) == false
//        }).map {
//            PhotoInfo(id: $0["id"] as! Int, url: $0["image_url"] as! String)
//        }
//        
//        self.photos.addObject(photoInfos)
//        print(self.photos)
//        
//        self.collectionView?.reloadData()
//        }
    }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }
  // MARK: CollectionView
  override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return photos.count
  }
  override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) as! PhotoBrowserCollectionViewCell
//    let imageURL = (photos.objectAtIndex(indexPath.row) as! PhotoInfo).url
    let imageURL = (photos.objectAtIndex(indexPath.row) as! PhotoInfo).url
    Alamofire.request(.GET, imageURL).response() {
        (_,_, data, _) in
        let image = UIImage(data: data!)
        cell.imageView.image = image
    }
    return cell
  }
  override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    return collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: PhotoBrowserFooterViewIdentifier, forIndexPath: indexPath) 
  }
  override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("ShowPhoto", sender: (self.photos.objectAtIndex(indexPath.item) as! PhotoInfo).id)
  }
  // MARK: Helper
  func setupView() {
    navigationController?.setNavigationBarHidden(false, animated: true)
    let layout = UICollectionViewFlowLayout()
    let itemWidth = (view.bounds.size.width - 2) / 3
    layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
    layout.minimumInteritemSpacing = 1.0
    layout.minimumLineSpacing = 1.0
    layout.footerReferenceSize = CGSize(width: collectionView!.bounds.size.width, height: 100.0)
    collectionView!.collectionViewLayout = layout
    navigationItem.title = "Featured"
    collectionView!.registerClass(PhotoBrowserCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: PhotoBrowserCellIdentifier)
    collectionView!.registerClass(PhotoBrowserCollectionViewLoadingCell.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: PhotoBrowserFooterViewIdentifier)
    refreshControl.tintColor = UIColor.whiteColor()
    refreshControl.addTarget(self, action: "handleRefresh", forControlEvents: .ValueChanged)
    collectionView!.addSubview(refreshControl)
  }
  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ShowPhoto" {
      (segue.destinationViewController as! PhotoViewerViewController).photoID = sender!.integerValue
      (segue.destinationViewController as! PhotoViewerViewController).hidesBottomBarWhenPushed = true
    }
  }
    // 1
    override func scrollViewDidScroll(scrollView: UIScrollView) {
        if scrollView.contentOffset.y + view.frame.size.height > scrollView.contentSize.height * 0.8 {
            populatePhotos()
        }
    }
    func populatePhotos() {
        // 2
        if populatingPhotos {
            return
        }
        populatingPhotos = true
        // 3
        Alamofire.request(Five100px.Router.PopularPhotos(self.currentPage)).responseJSON() {
            (_, _, JSON) in
                // 4
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
                    // 5, 6, 7
                    let photoInfos = ((JSON as! NSDictionary).valueForKey("photos") as! [NSDictionary]).filter ({
                        ($0["nsfw"] as! Bool) == false }).map { PhotoInfo(id: $0["id"] as! Int, url: $0["image_url"] as! String)}
                    //8
                    let lastItem = self.photos.count
                    //9
                    self.photos.addObject(photoInfos)
                    //10
                    let indexPaths = (lastItem..<self.photos.count).map { NSIndexPath(forItem: $0, inSection: $0) }
                    // 11
                    dispatch_async(dispatch_get_main_queue()) {
                        self.collectionView!.insertItemsAtIndexPaths(indexPaths)
                    }
                    self.currentPage++
            }
            self.populatingPhotos = false
        }
    }
  func handleRefresh() {
  }
}
class PhotoBrowserCollectionViewCell: UICollectionViewCell {
  let imageView = UIImageView()
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }
  override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor(white: 0.1, alpha: 1.0)
    imageView.frame = bounds
    addSubview(imageView)
  }
}
class PhotoBrowserCollectionViewLoadingCell: UICollectionReusableView {
  let spinner = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }
  override init(frame: CGRect) {
    super.init(frame: frame)
    spinner.startAnimating()
    spinner.center = self.center
    addSubview(spinner)
  }
}

答案很简单。在Five100px.swift文件中,您将看到静态let consumerKey。请更新consumerKey。如果你还活着的话。请从此链接下载新版本https://www.dropbox.com/s/e2unowffetv9h9c/Photomania.zip?dl=0

最新更新