I have a WKInterfacePicker in my Watch's Interface and want to add items to it. Here is my code I currently have:
let item1:WKPickerItem = WKPickerItem()
item1.title = "1"
let item2:WKPickerItem = WKPickerItem()
item2.title = "2"
timePicker.setItems([item1, item2])
When I run the app it crashes and tells me there is a nil last line:
fatal error: unexpectedly found nil while unwrapping an optional value
I had a look at Apple's docs for the WKInterfacePicker and WKPickerItem but that did not help me either. Is it possible that I have to set the content of the items somehow else? I thought the title is the content already and I couldn't find any other method for setting content.
It's likely that timePicker is nil. Did you remember to hook it up to the outlet?
#IBOutlet var timePicker: WKInterfacePicker!
Since it's declared with !, this is the only place you could be force unwrapping nil.
If you're still having problems, there's a full guide on WKInterfacePicker you can try here: http://www.sneakycrab.com/blog/2015/6/12/wkinterfacepicker-in-watchkit-20-using-the-digital-crown
Related
i have added a slider to my storyboard and connected it. the result is this code inside my "Viewcontroller.swift":
#IBOutlet var control1: UISlider!
when i try to change the value with this command:
control1.setValue(resultAvPitch, animated: false)
the app is crashing:
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value 2020-02-15 13:55:54.045989+0100 UIGrid[8695:898849] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value (lldb) –
i found somewhere a small code snippet and put it above this function:
let control1 = UISlider()
like that its not crashing anymore but it doesnt make the slider move.. what am i doing wrong?. is there an option that i need to click somewhere in the storyboard?
note: i placed the slider inside a view and another view like that:
what helped in the end was removing all sliders, drag and connect them again. i have checked muliple times what the differences were but i didnt find anything.
I am trying to add a button that allows the user to refresh the tableview and bring them back to the top of the table. I am trying to add a snippet of code that has a var set up like this var layoutGuide:UILayoutGuide! but it is giving me this message. I then try to run the simulator and simply ignore this message but then I get an error saying it has found an optional value nil when unwrapping it. Any ideas on why this is happening? Thank you
Use: let layoutGuide = self.view.safeAreaLayoutGuide instead of var layoutGuide:UILayoutGuide.
I'm trying to build city guide app from template but I'm unable to switch from sections view to just map view. As I understand this error means that code isn't quite finished and its missing something. Would gladly appreciate if anyone could help me with this one.
https://i.stack.imgur.com/cUCMO.png
As the debugger console has indicated that:
You're trying to cast value of type appyCity.SelectionsViewController to appyCity.MapPoiViewController. It's impossible => Crash
If you're not sure about the value of a property, let's use the Optional Chaining and Optional Binding instead of Forced Unwrapping To minimize the possibility of a crash.
Your code should look like this:
if let MPVC = segue.destination as? MapPoiViewController {
// MPVC is MapPoiViewController type
} else {
// MPVC isn't MapPoiViewController type
}
See more information here.
Hope this helps you!
Here a simple issue I am facing while starting to experiment with WatchKit and complications.
I created a simple app which is showing a complication with a public string “Y” and by clicking it, the Apple Watch app is shown with a simple switch.
I wrote my code in getPlaceholderTemplateForComplication in ComplicationController.swift and added a switch IBAction in InterfaceController.swift.
By changing the value of the switch, the public string cycles between “N” and “Y”. I would like to have it changed in the complication as well. However I am noticing that the complication stays as it is initially at “Y”.
I found a similar question about forcing complication updates, but it was related to a TimeLine complication func and not placeHolder.
func updateComplication() {
let complicationServer = CLKComplicationServer.sharedInstance()
for complication in complicationServer.activeComplications {
complicationServer.reloadTimelineForComplication(complication)
}
}
It is not clear to me on where and how to use this in my case.
As suggested I worked on getCurrentTimelineEntryForComplication .
In order to test a ModularComplication only, I used:
switch complication.family {
case .ModularSmall:
let modularSmallTemplate =
CLKComplicationTemplateModularSmallRingText()
modularSmallTemplate.textProvider =
CLKSimpleTextProvider(text: stringa)
modularSmallTemplate.fillFraction = 0.95
modularSmallTemplate.ringStyle = CLKComplicationRingStyle.Closed
let template = CLKComplicationTimelineEntry(
date: NSDate(), complicationTemplate: modularSmallTemplate)
handler(template)
default:
handler(nil)
}
I have the switch IBAction in InterfaceController.swift.
I am encountering issues in using:
func updateComplication() {
let complicationServer = CLKComplicationServer.sharedInstance()
for complication in complicationServer.activeComplications {
complicationServer.reloadTimelineForComplication(complication)
}
}
Where do I have to write the above mentioned func, in order to be able to call it from inside the IBAction?
If I write it in ComplicationController.swift, by calling it from the IBAction in InterfaceController.swift as:
ComplicationController.updateComplication()
I get the error “Missing argument for parameter #1 in call”,
while if I write it in InterfaceController and call it in the IBAction, although the build is successful, when running the App and changing the value of the switch I get the following error: “fatal error: unexpectedly found nil while unwrapping an Optional value” on the line:
for complication in complicationServer.activeComplications
of func updateComplication.
It's not the placeholder text that you want to update. The placeholder template is a static template that's displayed in the selection screen for your complication while you are customizing your watch face. It's only called once to get the placeholder text, and doesn't get called when a timeline is reloaded.
The complication's current (actual, live) timeline entry is provided by getCurrentTimelineEntryForComplication. This function is where you should use your model's on state of the switch to create a CLKTextProvider containing a "N" or "Y" for the active complication family.
Finally, you should call updateComplication() from your switch IBAction to reload your complication timeline, which would update the current entry to show the new state of the switch on the watch face.
Since your complication is updated manually, you should return a nil updateDate in getNextRequestedUpdateDateWithHandler to avoid scheduling your complication for any regular updates.
Update for your edited question:
You should include the updateComplication function in your interface controller, as your complication controller isn't meant to be instantiated by you.
In regard to the "Unexpectedly found nil while unwrapping an Optional value" error, this is an issue that has been mentioned on the Apple Developer Forums.
If you are already running watchOS 2.1, you could try the latest watchOS beta to see if the issue has been fixed yet for 2.2. You should also file a bug report and dupe radar 22947535.
I'm taking the Stanford iPad and iPhone developer course online at Stanford using Swift and working on creating a Calculator application. (Still a bit new to programming.)
However, it keeps crashing with the error message whenever I select an operator from the calculator.
operandStack = [36.0]
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
and then when I look in ViewController to see what part of the source code is highlighted. It is this:
NSNumberFormatter().numberFromString(display.text!)!.doubleValue }
the error message under this though, reads:
THREAD 1:EXC_BAD_INSTRUCTION(code:EXC_1386_INVOP,subcode=0x0)
I don't understand what's going on, since I entered the code, word for word from the lecture.
Any help is appreciated!
Thanks in advance!
I am stuck at that part of the course too!
I think that apple recently made changes to NSNumberFormatter's numberFromString method. Because when I printed out display.text!, there was nothing wrong. In other words, it does not found nil while unwrapping that part.
Another part we are unwrapping is here, at the second ! mark, we unwrap only this part:
NSNumberFormatter().numberFromString(display.text!)
But we have an error out of this, so numberFromString should be returning nil.
But in the videos, it doesn't. It perfectly turns floating point number strings (such as "36.0") to NSNumber, then to Double.
And since your question was asked on May 20th and I could not find any "old" questions, I think Apple had changed the code on numberFromString.
Edit: I did something crazy and used Find & Replace (command + F) to replace all "Double"s to "Int"s in my code. The multiplication part works well now, I think the problem is about the "." part on Doubles.
Edit 2: I solved it. Some countries such as US use "." to separate decimals and some others such as Turkey use "," to do it. It works on video because he's doing it on US.
NSNumberFormatter has a property called decimalSeparator. We have to set it to ".". I did the following changed to my code and it worked perfect.
var displayValue: Double {
get {
var formatter = NSNumberFormatter()
formatter.decimalSeparator = "."
return (formatter.numberFromString(display.text!)!.doubleValue)
}
set {
display.text = "\(newValue)"
userIsInTheMiddleOfTypingNumber = false
}
}
"found nil while unwrapping an Optional" means that you have a variable that may or maynot have a value, when you use the operator ! you are telling swift "Trust me there is a value in there" however if there is not swift will throw a exception as the one you just saw.
The best way to avoid this is checking before use:
if let value = display.text{
//if your code get here value is safe to use
NSNumberFormatter().numberFromString(value).doubleValue
}