Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ryan Doyle
8,587 PointsHelp with a practice Swift App!
Ok. I just finished make my first app through the Swift Track. It was great! (the randomly generating fact app).
So, I decided before moving on I would make another app that was similar to keep practicing and reinforce some of the newness of Xcode. So, I went for it and everything looks how I think it should be, but when I go to simulate the app it kicks back with an error. It shows some info in the debug, but I'm not far enough along to know what any of it means.
It's basically an app that randomly generates a student name (I'm a teacher) to call on them or what not. 3 buttons. 1 for anyone, and then a button for a random boy, or a random girl. Right now I've only done code for the main button. some screen shots are at the bottom, but here is the code:
StudentList.swift
import GameKit
class StudentName {
var name: String
var gender: String
required init(name: String, gender: String) {
self.name = name
self.gender = gender
}
}
struct StudentChoice {
let homeroom = [
StudentName(name: "Boy 1", gender: "Boy"),
StudentName(name: "Boy 2", gender: "Boy"),
StudentName(name: "Boy 3", gender: "Boy"),
StudentName(name: "Girl 1", gender: "Girl"),
StudentName(name: "Girl 2", gender: "Girl"),
StudentName(name: "Girl 3", gender: "Girl")
]
func getRandomStudent() -> StudentName {
let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(homeroom.count)
return homeroom[randomNumber]
}
}
ViewController
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var studentNameDisplay: UITextField!
let studentChoice = StudentChoice()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
studentNameDisplay.text = studentChoice.getRandomStudent().name
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func generateStudent() {
studentNameDisplay.text = studentChoice.getRandomStudent().name
}
}
DEBUG INFO 2016-07-20 16:51:40.931 Random Student Picker[12209:3746076] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Random_Student_Picker.ViewController 0x7fa031624210> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key chooseStudent.' *** First throw call stack: ( 0 CoreFoundation 0x0000000104af9d85 exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001068e9deb objc_exception_throw + 48 2 CoreFoundation 0x0000000104af99c9 -[NSException raise] + 9 3 Foundation 0x0000000104ecb19b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288 4 UIKit 0x0000000105500d0c -[UIViewController setValue:forKey:] + 88 5 UIKit 0x00000001057377fb -[UIRuntimeOutletConnection connect] + 109 6 CoreFoundation 0x0000000104a33890 -[NSArray makeObjectsPerformSelector:] + 224 7 UIKit 0x00000001057361de -[UINib instantiateWithOwner:options:] + 1864 8 UIKit 0x00000001055078d6 -[UIViewController _loadViewFromNibNamed:bundle:] + 381 9 UIKit 0x0000000105508202 -[UIViewController loadView] + 178 10 UIKit 0x0000000105508560 -[UIViewController loadViewIfRequired] + 138 11 UIKit 0x0000000105508cd3 -[UIViewController view] + 27 12 UIKit 0x00000001053defb4 -[UIWindow addRootViewControllerViewIfPossible] + 61 13 UIKit 0x00000001053df69d -[UIWindow _setHidden:forced:] + 282 14 UIKit 0x00000001053f1180 -[UIWindow makeKeyAndVisible] + 42 15 UIKit 0x0000000105365ed9 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131 16 UIKit 0x000000010536c568 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1769 17 UIKit 0x0000000105369714 -[UIApplication workspaceDidEndTransaction:] + 188 18 FrontBoardServices 0x00000001088098c8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK + 24 19 FrontBoardServices 0x0000000108809741 -[FBSSerialQueue performNext] + 178 20 FrontBoardServices 0x0000000108809aca -[FBSSerialQueue _performNextFromRunLoopSource] + 45 21 CoreFoundation 0x0000000104a1f301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION_ + 17 22 CoreFoundation 0x0000000104a1522c __CFRunLoopDoSources0 + 556 23 CoreFoundation 0x0000000104a146e3 __CFRunLoopRun + 867 24 CoreFoundation 0x0000000104a140f8 CFRunLoopRunSpecific + 488 25 UIKit 0x0000000105368f21 -[UIApplication _run] + 402 26 UIKit 0x000000010536df09 UIApplicationMain + 171 27 Random Student Picker 0x00000001049121e2 main + 114 28 libdyld.dylib 0x000000010743c92d start + 1 29 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
Jari Koopman
Python Web Development Techdegree Graduate 29,349 PointsHi Ryan,
Can you send a link or something to your complete project? I'd love to help you, the thin your app crashes on is a key that's not key value coding-compilant, but I don't see this key anywhere in your code. Are the code parts you included all the file's you made? Next to that, you might want to change your UITextField to an UILabel, a textField is editable by any user using your app and a label isn't.
Hope this helped so far!
Regards, Jari
1 Answer
Ryan Doyle
8,587 PointsHey Jari,
Here is a link to my project as a .zip
https://drive.google.com/file/d/0B10lVMzR3tVcZ0lQRmdobFI0dUk/view?usp=sharing
The code I included was everything that I made/added to the already existing Xcode stuff that it started off with as a single page app. The note you made about the UITextField and UILabel was helpful though. I wanted it to be a Label. Thanks so far!
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsHello :
I think you are passing a key somewhere ( "chooseStudent").. that might be causing this bug.
But I do not see it in your code.. is there anything else you are not showing on this code ?