1
votes

My app was working perfectly until I added an @IBAction. Now I just get this error:

2017-06-09 07:55:35.285 Press[1594:102051] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key add.' * First throw call stack: ( 0 CoreFoundation 0x000000010724bb0b exceptionPreprocess + 171 1 libobjc.A.dylib 0x00000001044f3141 objc_exception_throw + 48 2 CoreFoundation 0x000000010724ba59 -[NSException raise] + 9 3 Foundation 0x0000000104008e8b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292 4 UIKit 0x0000000104b60644 -[UIViewController setValue:forKey:] + 87 5 UIKit 0x0000000104dcd6b9 -[UIRuntimeOutletConnection connect] + 109 6 CoreFoundation 0x00000001071f1e8d -[NSArray makeObjectsPerformSelector:] + 269 7 UIKit 0x0000000104dcc06f -[UINib instantiateWithOwner:options:] + 1856 8 UIKit 0x0000000104b66c73 -[UIViewController _loadViewFromNibNamed:bundle:] + 381 9 UIKit 0x0000000104b67589 -[UIViewController loadView] + 177 10 UIKit 0x0000000104b678ba -[UIViewController loadViewIfRequired] + 195 11 UIKit 0x0000000104b6810a -[UIViewController view] + 27 12 UIKit 0x0000000104a3063a -[UIWindow addRootViewControllerViewIfPossible] + 65 13 UIKit 0x0000000104a30d20 -[UIWindow _setHidden:forced:] + 294 14 UIKit 0x0000000104a43b6e -[UIWindow makeKeyAndVisible] + 42 15 UIKit 0x00000001049bd31f -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4346 16 UIKit 0x00000001049c3584 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1709 17 UIKit 0x00000001049c0793 -[UIApplication workspaceDidEndTransaction:] + 182 18 FrontBoardServices 0x000000010895b5f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK + 24 19 FrontBoardServices 0x000000010895b46d -[FBSSerialQueue _performNext] + 186 20 FrontBoardServices 0x000000010895b7f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 21 CoreFoundation 0x00000001071f1c01 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 22 CoreFoundation 0x00000001071d70cf __CFRunLoopDoSources0 + 527 23 CoreFoundation 0x00000001071d65ff __CFRunLoopRun + 911 24 CoreFoundation 0x00000001071d6016 CFRunLoopRunSpecific + 406 25 UIKit 0x00000001049bf02f -[UIApplication _run] + 468 26 UIKit 0x00000001049c50d4 UIApplicationMain + 159 27 Press 0x0000000103f1a877 main + 55 28 libdyld.dylib 0x00000001081eb65d start + 1 29 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

This is my Viewcontroller code:

import UIKit

class ViewController: UIViewController {

// OUTLETS

@IBOutlet weak var score: UILabel!
@IBAction func add(_ sender: Any) {
    add()
}

// VARIABLES

var scoreVar = 0
let levelUpAt = [50, 100, 500, 1000, 5000, 10000, 50000, 100000]
var currentLevel = 1
var toAdd = 1

// OVERRIDES

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

// FUNCTIONS

// Below code adds to the score

func add() {
    scoreVar += 1 // Adds 1 to scoreVar
    score.text = "scoreVar"; // Updates text to match
    checkForLevelUp(); // Calls the function defined in the next few days ago
}

// Below code checks if the score meets the next level requirements

func checkForLevelUp() {
    if (scoreVar - 1 < levelUpAt[currentLevel - 1]) { // Complicated math-y if statment
        currentLevel += 1
        toAdd += 1
    }
}

}

It seems to be stuck on the class AppDelegate: UIResponder, UIApplicationDelegate { line on the AppDelegate, according to the debugger.

1
Check your Outlet on your storyboard, they are probably missing.brduca
yes seems outlet of your button not setted properly.if you modified the method name or param then probably have to remove old outlet also in viewDharma
I tried changing it to @IBAction func add(_ sender: UIButton, forEvent event: UIEvent) { add() } and no change. Any suggestions on how to connect properly?Elijah Ciali

1 Answers

1
votes

Go to your story board and click on the ViewController. Click one the Add button you created.

Add Button

On the right side in the Outlet Inspector, remove anything mentioning the "Add" button.

Outlets to remove

Finally, recreate the outlets by option dragging from the button to your view controller and setting the type to "Action".

New outlet to create