Ios NSUnknownKeyException UICollectionView

Ios NSUnknownKeyException UICollectionView,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我最近为我的应用程序实现了一个新的视图,它有一个UICollectionView作为图像加载到的网格: class MemoriesView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{ @IBOutlet weak var images_collection: UICollectionView! override func viewDidLoad() { super.

我最近为我的应用程序实现了一个新的视图,它有一个
UICollectionView
作为图像加载到的网格:

class MemoriesView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{

@IBOutlet weak var images_collection: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.navigationBarHidden = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("memory_image_1", forIndexPath: indexPath) as! CollectionViewController
        return cell
    }

}
现在,这个类中的代码很可能完全是自上而下的,因为我必须遵循这个教程。所以这里没有个人主动权。因此,当我尝试进入此视图时,可能会产生此错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Certi.MemoriesView 0x7fb613f3c8d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageCollectionView.'

*** First throw call stack:
(
    0   CoreFoundation                      0x000000010ee6ae65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010f2c7deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010ee6aaa9 -[NSException raise] + 9
    3   Foundation                          0x000000010cfb29bb -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
    4   UIKit                               0x000000010d98c320 -[UIViewController setValue:forKey:] + 88
    5   UIKit                               0x000000010dbbaf41 -[UIRuntimeOutletConnection connect] + 109
    6   CoreFoundation                      0x000000010edab4a0 -[NSArray makeObjectsPerformSelector:] + 224
    7   UIKit                               0x000000010dbb9924 -[UINib instantiateWithOwner:options:] + 1864
    8   UIKit                               0x000000010d992eea -[UIViewController _loadViewFromNibNamed:bundle:] + 381
    9   UIKit                               0x000000010d993816 -[UIViewController loadView] + 178
    10  UIKit                               0x000000010d993b74 -[UIViewController loadViewIfRequired] + 138
    11  UIKit                               0x000000010d999f4f -[UIViewController __viewWillAppear:] + 120
    12  UIKit                               0x000000010d9c9e44 -[UINavigationController _startCustomTransition:] + 1203
    13  UIKit                               0x000000010d9da23f -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
    14  UIKit                               0x000000010d9db3af -[UINavigationController __viewWillLayoutSubviews] + 57
    15  UIKit                               0x000000010db81ff7 -[UILayoutContainerView layoutSubviews] + 248
    16  UIKit                               0x000000010d8b44a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
    17  QuartzCore                          0x000000011101459a -[CALayer layoutSublayers] + 146
    18  QuartzCore                          0x0000000111008e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    19  QuartzCore                          0x0000000111008cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    20  QuartzCore                          0x0000000110ffd475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
    21  QuartzCore                          0x000000011102ac0a _ZN2CA11Transaction6commitEv + 486
    22  UIKit                               0x000000010d7f7f7c _UIApplicationHandleEventQueue + 7329
    23  CoreFoundation                      0x000000010ed96a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    24  CoreFoundation                      0x000000010ed8c95c __CFRunLoopDoSources0 + 556
    25  CoreFoundation                      0x000000010ed8be13 __CFRunLoopRun + 867
    26  CoreFoundation                      0x000000010ed8b828 CFRunLoopRunSpecific + 488
    27  GraphicsServices                    0x00000001115d7ad2 GSEventRunModal + 161
    28  UIKit                               0x000000010d7fd610 UIApplicationMain + 171
    29  Certi                               0x000000010b92c1fd main + 109
    30  libdyld.dylib                       0x000000010fde392d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合key imageCollectionView的键值编码。”
***第一次抛出调用堆栈:
(
0 CoreFoundation 0x000000010ee6ae65例外预处理+165
1 libobjc.A.dylib 0x000000010f2c7deb objc_异常_抛出+48
2 CoreFoundation 0x000000010ee6aaa9-[N异常提升]+9
3基金会0x000 000 010CFB29 BB-[ NSKORKS(NSKEY ValueCudid)设置值:FrKEY::+ 288)
4 UIKit 0x000000010d98c320-[UIViewController设置值:forKey:+88
5 UIKit 0x000000010dbbaf41-[UIRuntimeOutletConnection connect]+109
6 CoreFoundation 0x000000010edab4a0-[NSArray makeObjectsPerformSelector:+224
7 UIKit 0x000000010dbb9924-[UINib实例化所有者:选项:][1864
8 UIKit 0x000000010d992eea-[UIViewController\u loadViewFromNibNamed:bundle:][381
9 UIKit 0x000000010d993816-[UIViewController loadView]+178
10 UIKit 0x000000010d993b74-[UIViewController loadViewIfRequired]+138
11 UIKit 0x000000010d999f4f-[UIViewController\uuu视图将出现:][120
12 UIKit 0x000000010d9c9e44-[UINavigationController_startCustomTransition:+1203
13 UIKit 0x000000010d9da23f-[UINavigationController\U STARTDEFERREDTRANSION(如果需要):+712
14 UIKit 0x000000010d9db3af-[UINavigationController\uuu视图将布局子视图]+57
15 UIKit 0x000000010db81ff7-[UILayoutContainerView布局子视图]+248
16 UIKit 0x000000010d8b44a3-[UIView(CALayerDelegate)布局层的子层:+703
17夸脱核心0x0000000111101459A-[CALayer布局子层]+146
18夸脱核心0x0000000111008e70(如果需要)Zn2Ca5层布局交易+366
19 QuartzCore 0x0000000111008cee\u ZN2CA58层布局和显示(如果需要)PNS\u 11事务+24
20夸脱核心0x0000000110ffd475_ZN2CA7Context18提交交易PNS_11交易+277
21夸脱核心0x000000011102ac0a_Zn2Ca11交易承诺+486
22 UIKit 0x000000010D7F7C_UIU应用程序句柄VENTQUEUE+7329
23 CoreFoundation 0x000000010ed96a31\uuuu CFRUNLOOP\u正在调用\u OUT\u以执行\u函数\uuu+17
24 CoreFoundation 0x000000010ed8c95c\uU CFRunLoopDoSources0+556
25 CoreFoundation 0x000000010ed8be13\uu CFRunLoopRun+867
26 CoreFoundation 0x000000010ed8b828 CFRunLoopRunSpecific+488
27图形服务0x00000001115d7ad2 GSEventRunModal+161
28 UIKit 0x000000010d7fd610 UIApplicationMain+171
29证书0x000000010b92c1fd主管道+109
30 libdyld.dylib 0x000000010fde392d开始+1
)
libc++abi.dylib:以NSException类型的未捕获异常终止

此函数就是问题所在:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("memory_image_1", forIndexPath: indexPath) as! CollectionViewController

return cell
您正在尝试将
UICollectionViewCell
转换为
CollectionViewController
。对该行末尾的简单更改:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("memory_image_1", forIndexPath: indexPath) as! UICollectionViewCell
// see below
return cell
将防止此错误,但仍会得到空白单元格。我添加注释
\\see below
的地方是您需要更新
单元格的值以选择此行显示的内容的地方。如果使用多个节,则可以通过
indexPath.row
indexPath.section
确定行

此外,如果要为单元格使用自定义类来处理默认视图以外的视图,则需要将
cell
强制转换到该自定义类,而不是
UICollectionViewController
,虽然我认为既然你还在看教程,那么你现在还不打算尝试


正如我在对这个问题的评论中所说,本教程和本网站上的其他教程对初学者来说非常好。

此功能就是问题所在:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("memory_image_1", forIndexPath: indexPath) as! CollectionViewController

return cell
您正在尝试将
UICollectionViewCell
转换为
CollectionViewController
。对该行末尾的简单更改:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("memory_image_1", forIndexPath: indexPath) as! UICollectionViewCell
// see below
return cell
将防止此错误,但仍会得到空白单元格。我添加注释
\\see below
的地方是您需要更新
单元格的值以选择此行显示的内容的地方。如果使用多个节,则可以通过
indexPath.row
indexPath.section
确定行

此外,如果要为单元格使用自定义类来处理默认视图以外的视图,则需要将
cell
强制转换到该自定义类,而不是
UICollectionViewController
,虽然我认为既然你还在看教程,那么你现在还不打算尝试

正如我在对这个问题的评论中所说的,本教程和本网站上的其他教程对初学者来说非常好。