I have a little problem with my code so that I can't call a function. I created a function func changeLabelText(text: String) { mylabel.stringValue = text }
in the ViewController with that I can update the Text in the Label. When I try to call the function from another class in the Project, I get a runtime error(EXC_BAD_Instr...
) and the Debugger holds on the line where I try to change the Label's text, with the error: fatal error: unexpectedly found nil while unwrapping an Optional value
. Where is the problem? Can someone help me please!

- 744
- 1
- 9
- 27
-
1Update your question with relevant code. – rmaddy Aug 23 '15 at 16:54
-
Can you provide details of the error you see? Including the line and error message. My initial suspicion is that this is related to the queue you're using to call the method, but details of the error should give a better clue. – Tom Elliott Aug 23 '15 at 17:30
2 Answers
Regarding the "unexpectedly found nil while unwrapping an Optional value" error, the problem is that you have an optional (judging from the code snippet you've provided, it must be an implicitly unwrapped optional) that is nil
. Most likely, mylabel
is nil
. Either print the value or add a breakpoint and examine the property in the debugger to confirm.
If it is nil
, you then have to figure out why. In our discussion, it turns out that you're trying to call the Log
event in the view controller:
ViewController().Log("asdf")
The problem is that this doesn't call Log
in the existing view controller, but rather the ViewController()
expression ends up instantiating a new, completely unrelated view controller with its outlets not hooked up to anything. Thus the attempt to update the implicitly unwrapped outlet will cause the error you shared with us.
If you want this separate class (a database manager object) to inform the view controller of event in order to allow the view controller to update the UI, there are three common approaches:
Completion/progress handlers that are closures/blocks.
This is used for simple interface where the database needs to inform view controller when the request is done.
Delegate-protocol pattern.
The delegation pattern (usually conforming to some well-established protocol) is used for rich interfaces where the database needs to inform the view controller of a variety of different types of events.
Notification pattern.
Notifications are used when you want a loosely coupled interface between the database object and whatever is handling these notifications. The view controller might register itself as an observer of any notifications of a particular name with the
defaultCenter()
of theNSNotificationCenter
. The database object can then post notifications of that name (supplying details via theuserInfo
dictionary) and the view controller will be informed of these events.

- 415,655
- 72
- 787
- 1,044
-
@Michael_mhr - Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87812/discussion-between-rob-and-michael-mhr). – Rob Aug 24 '15 at 16:53
A few things are you calling an instance of the view controller properly
e.g if the class name is ViewController, in the the view controller that you are calling the function make sure you use
let VC = ViewController ()
and then use VC.yourFunctionName
also instead of stringValue use .text

- 94
- 1
- 9
-
Re `stringValue` vs `text`, originally this question was tagged as iOS, so you are correct that `UILabel` uses `text`. The OP has clarified/corrected question, making it clear that it was `NSLabel` in Mac OS X, in which case `stringValue` is correct. – Rob Aug 24 '15 at 05:37
-
Also, the `let vc = ViewController()` pattern is almost certainly not right: First, he might not even have VC in the Mac OS project. Second, instantiating a VC without referencing the NIB and/or storyboard identifier is a common _source_ of this sort of problem, not a fix for it. We would need to know a lot more about the design of the app before we went down this road. – Rob Aug 24 '15 at 05:54