I am learning Swift using a book: iOS Programming for Beginners.
I have an error that I can't see why I am getting it. I am following the book and can't see where I have made a mistake. I even copied the code from the download that accompany the book, but I still get an error.
Here is the error:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
<#code#>
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
<#code#>
}
Error:
2021-03-11 18:15:42.397261+0000 LetsEat2[2739:121330] *** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], UICollectionView.m:6379
2021-03-11 18:15:42.399561+0000 LetsEat2[2739:121330] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier restaurantCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(
0 CoreFoundation 0x00000001803f61fc __exceptionPreprocess + 236
1 libobjc.A.dylib 0x000000018016a438 objc_exception_throw + 56
2 CoreFoundation 0x00000001803f6060 +[NSException raise:format:] + 0
3 Foundation 0x000000018074983c -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 128
4 UIKitCore 0x0000000183b6924c -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 2156
5 UIKitCore 0x0000000183b693d0 -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 100
6 LetsEat2 0x00000001009c9d04 $s8LetsEat228RestaurantListViewControllerC010collectionE0_13cellForItemAtSo012UICollectionE4CellCSo0lE0C_10Foundation9IndexPathVtF + 276
7 LetsEat2 0x00000001009c9ddc $s8LetsEat228RestaurantListViewControllerC010collectionE0_13cellForItemAtSo012UICollectionE4CellCSo0lE0C_10Foundation9IndexPathVtFTo + 164
8 UIKitCore 0x0000000183b563ec -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 408
9 UIKitCore 0x0000000183b5af54 -[UICollectionView _updateVisibleCellsNow:] + 5520
10 UIKitCore 0x0000000183b5f4ec -[UICollectionView layoutSubviews] + 320
11 UIKitCore 0x00000001848ccfa4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2540
12 QuartzCore 0x0000000187676384 -[CALayer layoutSublayers] + 288
13 QuartzCore 0x000000018767c870 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 504
14 QuartzCore 0x0000000187687938 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124
15 QuartzCore 0x00000001875df028 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 412
16 QuartzCore 0x0000000187608f94 _ZN2CA11Transaction6commitEv + 732
17 QuartzCore 0x000000018760a16c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 96
18 CoreFoundation 0x0000000180364190 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 32
19 CoreFoundation 0x000000018035e90c __CFRunLoopDoObservers + 556
20 CoreFoundation 0x000000018035ee7c __CFRunLoopRun + 992
21 CoreFoundation 0x000000018035e58c CFRunLoopRunSpecific + 572
22 GraphicsServices 0x000000018b9c2740 GSEventRunModal + 160
23 UIKitCore 0x00000001843ccbf0 -[UIApplication _run] + 964
24 UIKitCore 0x00000001843d19d0 UIApplicationMain + 112
25 libswiftUIKit.dylib 0x00000001b220f328 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 100
26 LetsEat2 0x00000001009c8f28 $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 120
27 LetsEat2 0x00000001009c8ea0 $s8LetsEat211AppDelegateC5$mainyyFZ + 48
28 LetsEat2 0x00000001009c8f6c main + 32
29 libdyld.dylib 0x0000000180223cbc start + 4
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier restaurantCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
terminating with uncaught exception of type NSException
CoreSimulator 732.18.6 - Device: iPhone SE (2nd generation) (CB044DEA-0C3E-475F-BB23-944A89A8280E) - Runtime: iOS 14.4 (18D46) - DeviceType: iPhone SE (2nd generation)
(lldb)
Here is the code:
//
// RestaurantListViewController.swift
// LetsEat2
//
// Created by Tony Hudson on 11/03/2021.
//
import UIKit
class RestaurantListViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(withReuseIdentifier: "restaurantCell", for: indexPath)
}
}
I builds OK but gives this error when I click the cell in RestaurantViewController.
Can anyone help?