1

Good day everyone, I need your help on the following : I have a swift file where I store different functions used by different Class swift files.

Here is a part of one of these functions:

            strStatus = "STEP 1/8 : Downloading data..."

            if myView == "C" {
                CDetails().lblStatus1.text = strStatus // Here is the error
            }
            else if myView == "A" {
                Alerts().lblStatus4.text = strStatus
            }

Then I have 2 Class, CDetails and Alerts, which can call this function. Depending on the Class, a message is to be displayed in a label. However, when I execute the code I get the following error: Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

I read different topics related to this problem but was not able to sort it out. So I would be grateful if you could be of any help! I thank you in advance for your assistance on this!

hgiraud
  • 23
  • 3
  • There's a question with a near duplicate title. Are you sure that's not the solution? Sounds like your property can't be `nil` but it is. – tadman Jan 08 '21 at 12:46
  • what is your CDetails() inside? – Picode Jan 08 '21 at 12:57
  • 2
    `CDetails()`, that's a UIView or a UIViewController? You know that's creating a new one, and not the one that you might see on screen? Also, `lblStatus1` or `lblStatus4` wasn't load yet (I guess it's an IBOutlet). So could you clarify a little the screens logic here? – Larme Jan 08 '21 at 13:31
  • Does this answer your question? [What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Joakim Danielson Jan 08 '21 at 13:37
  • CDetails is a UIViewController. Now I realize that with () I was creating a new one. However I cannot figure out the way to point to lblStatus1 on CDetails. Indeed when I write CDetails followed by a point, I do not have any lblStatus member choice after the point. How can I designate lblStatus (which is a UILabel) located on CDetails (which is a UIViewController) when I call it from out of CDetails class? – hgiraud Jan 08 '21 at 13:56
  • Where is `CDetails`, where is `Alerts`? What's the logic between them and the current code? Where is your current code? In the "previous view controller"? A later one? A non-viewcontroller? Etc. – Larme Jan 08 '21 at 15:33
  • Thank you Larme for your follow up, much appreciated! From CDetails viewController when I click on a cmd button it calls a function located on a non-viewController in another swift file. I do that because the called function is used by different viewControllers and I do not want to copy paste this function to all of them. Then, depending on the viewController who called this function (e.g CDetails, Alerts or others), I need to access to their own UITextFields, UILabels). Problem is : From a non-viewController, when I write CDetails I have no TextField nor Labels members after the point. – hgiraud Jan 08 '21 at 15:54

0 Answers0