I am trying to learn Xcode using a book called 'iOS 14 Programming for Beginners'. Although the book is supposed to show the correct code I have found that it still raises errors when I type in the code. They also have the code online, which I have downloaded and compared to my code, yet I still get errors. Here is the file that produces the errors:
//
// ExploreViewController.swift
// EatOut
//
// Created by Tony Hudson on 15/07/2021
//
import UIKit
class ExploreViewController: UIViewController,
UICollectionViewDelegate {
@IBOutlet weak var collectionView:
UICollectionView!
let manager = ExploreDataManager()
var selectedCity: LocationItem?
var headerView: ExploreHeaderView!
override func viewDidLoad() {
super.viewDidLoad()
initialize()
}
override func prepare(for segue:
UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case
Segue.locationList.rawValue:
showLocationList(segue: segue)
case
Segue.restaurantList.rawValue: //ERROR
HERE
showRestaurantListing(segue: segue)
default:
print("Segue not added")
}
}
override func
shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool
{
if identifier ==
Segue.restaurantList.rawValue { //AND HERE
guard selectedCity!= nil else {
showAlert()
return false
}
return true
}
return true
}
}
// MARK: Private Extension
private extension ExploreViewController {
func initialize() {
manager.fetch()
}
func showLocationList(segue:
UIStoryboardSegue) {
guard let navController =
segue.destination as? UINavigationController, let viewController =
navController.topViewController as? LocationViewController else {
return
}
guard let city = selectedCity
else {
return
}
viewController.selectedCity = city
}
func showRestaurantListing(segue:
UIStoryboardSegue) {
if let viewController =
segue.destination as? RestaurantListViewController, let city = selectedCity,
let index = CollectionView.indexPathsForSelectedItems?.first {
viewController.selectedType = manager.explore(at: index).name
viewController.selectedCity = city
}
}
func showAlert() {
let alertController = UIAlertController(title:
"Location Needed", message: "Please select a location.",
preferredStyle: .alert)
let okAction =
UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
present(alertController,
animated: true, completion: nil)
}
@IBAction func unwindLocationCancel(segue:
UIStoryboardSegue){
}
@IBAction func
unwindLocationDone(segue:UIStoryboardSegue) {
if let viewController =
segue.source as? LocationViewController {
selectedCity =
viewController.selectedCity
if let location =
selectedCity {
headerView.lblLocation.text = location.full
}
}
}
}
// MARK: UICollectionViewDataSource
extension ExploreViewController: UICollectionViewDataSource
{
func collectionView(_ collectionView:
UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath:
IndexPath) -> UICollectionReusableView {
let header =
collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: "header", for: indexPath)
headerView = header as?
ExploreHeaderView
return headerView
}
func collectionView(_ collectionView:
UICollectionView, numberOfItemsInSection section: Int) -> Int {
manager.numberOfItems()
}
func collectionView(_ collectionView:
UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell =
collectionView.dequeueReusableCell(withReuseIdentifier:
"exploreCell", for: indexPath) as! ExploreCell
let item = manager.explore(at:
indexPath)
cell.lblName.text = item.name
cell.imgExplore.image = UIImage(named:
item.image)
return cell
}
//
Can anyone see why this is producing this error?```