2

I know there are similar questions out there and I have looked at them but I can't seem to solve the problem. I'm actually working on this project here that Apple uses to help users learn Xcode: https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ConnectTheUIToCode.html#//apple_ref/doc/uid/TP40015214-CH22-SW1

This is my ViewController.swift code:

import UIKit

class ViewController: UIViewController {
    //MARK: Properties
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var mealNameLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a 
        nib.
    }


    //MARK: Actions
    @IBAction func setDefaultLabelText(_ sender: UIButton) {
        mealNameLabel.text = "Default Text"
    }
}

I keep getting this error even when I follow all of the steps:

2017-10-14 16:18:49.195098-0400 FoodTracker[1946:46609] [MC] Lazy loading 
NSBundle MobileCoreServices.framework
2017-10-14 16:18:49.195968-0400 FoodTracker[1946:46609] [MC] Loaded 
MobileCoreServices.framework
2017-10-14 16:18:49.211086-0400 FoodTracker[1946:46609] [MC] System group 
container for systemgroup.com.apple.configurationprofiles path is 
/Users/xcodeclub/Library/Developer/CoreSimulator/Devices/54569EE9-2F14-41C6-
94D7-EDB605459B46/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-10-14 16:18:49.221000-0400 FoodTracker[1946:46609] *** Terminating app 
due to uncaught exception 'NSUnknownKeyException', reason: 
'[<FoodTracker.ViewController 0x7fc93bf08b80> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key setDefaultLabelText.'

Anybody care to help?

1 Answers1

4

I think that you have 2 outlets for the same label or textfield. You should check it in storyboard (right click on View Controller) . That's how it should look at the end of this part of the tutorial.

Click here

Make sure that there is no warnings.

  • Is there any way you can point out in the code I posted that I might have two outlets for the same label or text field? From what I see I see one for a text field and one for a label, but I could be missing something. Also, where do I get the black View Controller window you put up there? – Sungmin Gan Oct 15 '17 at 07:14
  • 1
    No, code seems to be okay, but it's common mistake. You have probably made outlet with wrong name (or it wasn't correct from any other reason) and then you deleted it from code and create a new one. The problem is that after you delete outlet or action from code you should disconnect/delete it in storyboard too. Actually, I was able to reproduce similar error. Here is screenshot where you can clearly see how to check it. [link] (https://i.imgur.com/EzIEotN.jpg) If you have there any yellow warnings you should use "X" to disconnect the outlet. And then connect again with your code. – Gienadij Mackiewicz Oct 15 '17 at 15:24
  • So the text fields and labels seem fine but I get a warning for the setDefaultLabelText action: "ViewController does not have an outlet named setDefaultLabelText." – Sungmin Gan Oct 15 '17 at 17:57
  • So i suppose you've tried re-connecting it to your action in ViewController and it doesn't help. You should definitely check these answers [First](https://stackoverflow.com/questions/26203970/xcode-6-cant-connect-any-iboutlet-to-viewcontroller/) and [Second](https://stackoverflow.com/questions/25931865/error-in-xcode-6-view-controller-does-not-have-an-outlet-named-subview). – Gienadij Mackiewicz Oct 15 '17 at 18:27
  • Haha weird...since the error is "ViewController does not have an outlet named setDefaultLabelText" I just added an outlet called setDefaultLabelText in addition to the action function and it seems to work. The warning goes away and when I run the simulator the app does what it's supposed to do. Do you think having the extra outlet is going to cause problems later down the road? – Sungmin Gan Oct 15 '17 at 18:36
  • You should check if "Set Default Label Text" button have only one outlet in the Connections Inspector If yes there should be no more issues. [Check the link](https://i.imgur.com/ko4Mv1c.png) – Gienadij Mackiewicz Oct 15 '17 at 18:56
  • 1
    Yeah looks like there's just one! Thanks for all your help! I can move forward with the tutorial now :) – Sungmin Gan Oct 15 '17 at 20:05