gpt4 book ai didi

ios - 线程 5 : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) - 从 'Result' 转换为无关类型 'NSDictionary' 总是失败

转载 作者:行者123 更新时间:2023-11-28 08:57:50 24 4
gpt4 key购买 nike

我正在关注关于 Alamofire 的 Ray Wenderlich 教程(并且正在努力),http://www.raywenderlich.com/85080/beginning-alamofire-tutorial#comments ,在“加载更多照片”部分的标题中出现错误。

错误在“let photoInfos = ((JSON as!NSDictionary).valueForKey("photos") as![NSDictionary]).filter ({"

这是我的代码。

//
// 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 文件中,你会看到 static let consumerKey。请更新 consumerKey。如果你活着继续同样的问题。请下载新版本https://www.dropbox.com/s/e2unowffetv9h9c/Photomania.zip?dl=0从这个链接。

关于ios - 线程 5 : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) - 从 'Result<AnyObject>' 转换为无关类型 'NSDictionary' 总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32637008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com