How to manually assign an Image to UIImageView - swift

I am trying to add an Image to UIImageView programmatically.
I found a code from here that I've tried a few times but it stopped working for some reason.
popupImage.image=UIImage(named: "juice")
I get this error message:
"Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"

Related

Swift getting Nil when attempting to get value from key that exists [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Im not sure why i am getting this nil error on optional when attempting to print. Can somebody provide any input? It doesn't make sense that I am able to output the value from the aplicationContext, however when I attempt to get value from applicationContext["hearRate"] i get nil.
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
print("didReceiveApplicationContext\n\t\(applicationContext)")
let hrValue = applicationContext["heartRate"] as? String
print(applicationContext["hearRate"])
print(hrValue)
heartRate.text = hrValue
}
console - output
2021-03-10 18:41:04.623716-0700 Trainer+[1824:759737] Metal API Validation Enabled
session active state
didReceiveApplicationContext
["heartRate": 00BPM]
nil
Optional("00BPM")
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file Trainer_/ViewController.swift, line 57
2021-03-10 18:41:04.999001-0700 Trainer+[1824:759754] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file Trainer_/ViewController.swift, line 57
Update - Ignore typo, found issue with IBOutlet
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
print("didReceiveApplicationContext\n\t\(applicationContext)")
let hrValue = applicationContext["heartRate"] as? String
print(applicationContext["heartRate"])
print(hrValue)
//heartRate.text = hrValue
heartRate.text = "test"
}
2021-03-11 12:45:05.482464-0700 Trainer+[1873:817768] Metal API Validation Enabled
session active state
didReceiveApplicationContext
["heartRate": 91BPM]
Optional(91BPM)
Optional("91BPM")
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file Trainer_/ViewController.swift, line 58
2021-03-11 12:45:05.887523-0700 Trainer+[1873:817966] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file Trainer_/ViewController.swift, line 58
the func session(didReceiveApplicationContext), from watchconnectivity is working as intended. I believe that my problem comes from me moving the heartRate label to a 'view container'. I added the watchconnectivity at the same time that I moved the label and assumed that it would work.
This may require a different question, not sure, how can i pass data along multiple view controllers that are visible at the same time. Since I am creating the session on the ViewController, I don't yet know how to pass the data to other view controllers without passing a full 'self' reference of the ViewController, to the DetailViewController. Coming from Java and PHP, i feel like this is not good coding practice.
I am thinking about extensions/delegates, and prototypes. I understand them to a slight degree, but I am currently working on an app and my main goal is to have a working prototype. I'll come back and refactor the code fix and fix any gaping vulnerabilities. If any body with experience can provide me with any reliable resources. Tired of hitting the next page of google as i cant find the answers that I am looking for lol.
If you have copied and pasted your code, I think it's just misspelling.
You may try changing this line:
print(applicationContext["hearRate"])
to:
print(applicationContext["heartRate"])

Swift PDFView issue: .notdef: no mapping

I get odd error messages with PDFView from PDFKit.
In some instance I get
non-isolated groups aren't supported.
Mutating glyph 1 (code 32) in ULBEWJ+MinionPro-Regular.
In another instance I get the message:
.notdef: no mapping.
In most PDF's I display I get no error messages.
The same for all PDFs is that they're all shown correctly in simulator.
Does this has something to do with UTF?
Any idea how to fix those error messages?

setValue not moving UISlider

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.

Getting error due to item found nil during unwrapping of Optional Value

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.

WKinterfacePicker item issue

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