Food Tracker Tutorial Error

Hi all, I'm new to all this so please keep that in mind while reading.


This is the code I am using; the file refuses to build because of line 16. The error says "Cannot assign value of type 'ViewController' to type 'UITextFieldDelegate?'".


//
//  ViewController.swift
//  FoodTracker
//
//

import UIKit

class ViewController: UIViewController {
    //MARK: Properties
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var mealNameLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        nameTextField.delegate = self
        // Handle the text field’s user input through delegate callbacks.
    }
    //MARK: UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        //Hide the keyboard
        textField.resignFirstResponder()
        
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        mealNameLabel.text = textField.text
    }
    
    //MARK: Actions
    @IBAction func setDefaultLabelText(_ sender: UIButton) {
        mealNameLabel.text = "Default Text"
    }
    
}


Here is the full error output message.

020-04-09 14:19:12.897243-0400 FoodTracker[29402:868329] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FoodTracker.ViewController 0x7ffaef406830> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key nameTypeField.'

*** First throw call stack:

(

0 CoreFoundation 0x00007fff23e3dcce __exceptionPreprocess + 350

1 libobjc.A.dylib 0x00007fff50b3b9b2 objc_exception_throw + 48

2 CoreFoundation 0x00007fff23e3d899 -[NSException raise] + 9

3 Foundation 0x00007fff258d5796 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 325

4 UIKitCore 0x00007fff485689b6 -[UIViewController setValue:forKey:] + 87

5 UIKitCore 0x00007fff48874ad3 -[UIRuntimeOutletConnection connect] + 109

6 CoreFoundation 0x00007fff23e29fe2 -[NSArray makeObjectsPerformSelector:] + 242

7 UIKitCore 0x00007fff48871cbc -[UINib instantiateWithOwner:options:] + 2192

8 UIKitCore 0x00007fff4856fdbe -[UIViewController _loadViewFromNibNamed:bundle:] + 379

9 UIKitCore 0x00007fff485708f5 -[UIViewController loadView] + 177

10 UIKitCore 0x00007fff48570bf4 -[UIViewController loadViewIfRequired] + 172

11 UIKitCore 0x00007fff485713a1 -[UIViewController view] + 27

12 UIKitCore 0x00007fff48c51ae1 -[UIWindow addRootViewControllerViewIfPossible] + 326

13 UIKitCore 0x00007fff48c5110a -[UIWindow _updateLayerOrderingAndSetLayerHidden:actionBlock:] + 219

14 UIKitCore 0x00007fff48c52195 -[UIWindow _setHidden:forced:] + 362

15 UIKitCore 0x00007fff48c655bc -[UIWindow _mainQueue_makeKeyAndVisible] + 42

16 UIKitCore 0x00007fff48e84d2c -[UIWindowScene _makeKeyAndVisibleIfNeeded] + 202

17 UIKitCore 0x00007fff48177720 +[UIScene _sceneForFBSScene:create:withSession:connectionOptions:] + 1405

18 UIKitCore 0x00007fff48c15561 -[UIApplication _connectUISceneFromFBSScene:transitionContext:] + 1019

19 UIKitCore 0x00007fff48c15898 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 291

20 UIKitCore 0x00007fff4876a160 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361

21 FrontBoardServices 0x00007fff36c6091e -[FBSSceneImpl _callOutQueue_agent_didCreateWithTransitionContext:completion:] + 419

22 FrontBoardServices 0x00007fff36c869b1 __86-[FBSWorkspaceScenesClient sceneID:createWithParameters:transitionContext:completion:]_block_invoke.154 + 102

23 FrontBoardServices 0x00007fff36c6b347 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 220

24 FrontBoardServices 0x00007fff36c86642 __86-[FBSWorkspaceScenesClient sceneID:createWithParameters:transitionContext:completion:]_block_invoke + 355

25 libdispatch.dylib 0x00000001075eee8e _dispatch_client_callout + 8

26 libdispatch.dylib 0x00000001075f1da2 _dispatch_block_invoke_direct + 300

27 FrontBoardServices 0x00007fff36cac2d9 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30

28 FrontBoardServices 0x00007fff36cabfc7 -[FBSSerialQueue _queue_performNextIfPossible] + 441

29 FrontBoardServices 0x00007fff36cac4d6 -[FBSSerialQueue _performNextFromRunLoopSource] + 22

30 CoreFoundation 0x00007fff23da1c71 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17

31 CoreFoundation 0x00007fff23da1b9c __CFRunLoopDoSource0 + 76

32 CoreFoundation 0x00007fff23da13cc __CFRunLoopDoSources0 + 268

33 CoreFoundation 0x00007fff23d9bf6e __CFRunLoopRun + 974

34 CoreFoundation 0x00007fff23d9b884 CFRunLoopRunSpecific + 404

35 GraphicsServices 0x00007fff38b5ac1a GSEventRunModal + 139

36 UIKitCore 0x00007fff48c19220 UIApplicationMain + 1605

37 FoodTracker 0x000000010737880b main + 75

38 libdyld.dylib 0x00007fff519b910d start + 1

39 ??? 0x0000000000000001 0x0 + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException


Thanks for your help in advance.

Replies

Food Tracker has always been a broken toy, with built-in mistakes. This has been gone over several times in the past several years, so your best self-help is to simply start searching on: food tracker


As an example, I believe the solution you need at this time was previously covered in this 5 yr. old thread, so no need to repeat the correct code here, again:


Food Tracker - Tutorial problem


My advice, otherwise, is to run, not walk, away from the FT tutorial, drop it like a hot rock, and find something that won't force you to waste time attempting to fix outdated crap when you should be learning with comfort, instead.


...good luck.

As KMT pointed out, FoodTracker is badly bugged.


You'll get a lot more errors.


This one is easy to solve:

change:

class ViewController: UIViewController {

into

class ViewController: UIViewController, UITextFieldDelegate {


Now, self is a UITextFieldDelegate and you can set the delegate of the textField to it.


That simple.