1

I'm making a simple temperature converter for a mobile apps class using Swift. We're sort of breezing over swift so I don't have a lot of experience with it. I had a lot of problems getting it to compile but I eventually got everything all nice and neat, no warning or errors of any sort, and I'm happy with my MVC style code. The only problem is that when I enter a temp and click the Convert button, my log checkpoints prove that I'm getting he correct double value for conversion, and I caste it as a String, but when I try to change the outputText label I get the fatal error: unexpectedly found nil while unwrapping an Optional value. I was hoping it's something very simple that a second pair of eyes could catch. My app looks like this...

App view

My ViewController is...

import UIKit

class ViewController: UIViewController
{

    @IBOutlet weak var tempInput: UITextField!
    @IBOutlet weak var tempOutput: UILabel!
    @IBOutlet weak var convertMethod: UISwitch!

    override func viewDidLoad()
    {
        print("view load confirmed");
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func convert(sender: AnyObject!)
    {
        print ("button press confirmed");

        let t = Double(tempInput.text!)
        let m = convertMethod.on
        let result = convertModel(temp:t!,method:m)
        print("result has been set to object variable created as stated above");

        let afterConvert = result.convertTemp()
        print("the convertTemp method was called and the result was stored as the double: \(afterConvert)");

        let finalForm = String(afterConvert)
        print("converted the double into the string: \(finalForm)");

        tempOutput.text = finalForm
        //^^^^^^^ this is the line that fails
    }
}

and my convertModel class (i know, the class should be capitalized, by the time I noticed and tried to change it, it broke everything) is...

import Foundation
class convertModel
{
    var temp:Double?
    var method:Bool?

    init(temp:Double,method:Bool)
    {
        self.temp = temp
        self.method = method
        print("object of type convertModel created with \(temp) as temp and \(method) as method");
    }

    func convertTemp()->Double
    {
        print("convertTemp method entered");

        if method == true
        {
            print("if conditional entered");
            print("conversion completed, exiting convertTemp method");

            return (temp! * (9/5) + 32)
        }
        else
        {
            print("else conditional entered");
            print("conversion completed, exiting convertTemp method");
            return (temp! - 32) * (5/9)
        }
    }
}

Any help is greatly appreciated. But please bear in mind that this is very Swift 101, so any overly complicated solutions don't help greatly. Thanks.

Andrew-Saarima
  • 270
  • 3
  • 9
  • 1
    did u connect the outlet in the storyboard ? Did u check your method / text field doesn't return nil ? – Teja Nandamuri Nov 18 '15 at 22:48
  • 1
    My first guess is your button is not hooked up. There are 2 common ways to test that your code is working as expected: breakpoints and print statements. Just add some print statements to your code and see when they stop happening. I would suggest your first print() be at the beginning of your function that is called on a button press – Tyrelidrel Nov 18 '15 at 22:50
  • Apparently it's not hooked up correctly. I put a few print statements in and I see the one on viewDidLoad, but nothing when I click button. Is there a way to connect them after the fact? I tried to create another button connecting the way I learned and the only thing in the click method is a print statement, it crashes the app with no error message, just shows the print statement line and says Thread 1: breakpoint 3.1 – Andrew-Saarima Nov 18 '15 at 23:10
  • 1
    To connect them after the fact open up the side by side editor and ctrl drag from your button to the action method. Also make sure that you only have references you want on everything. – Tyrelidrel Nov 18 '15 at 23:14
  • Yup, that did something, now the print statement in my click method and in my ConvertModel constructor both print, but then the thread breaks somewhere after that. I'll have to look into that more, anyway, at least I know the buttons working, thanks! – Andrew-Saarima Nov 18 '15 at 23:39
  • Ok, so I've got it working until the very end, the last step where I would change the text of my output label to the double that was returned (and verified by checkpoints) from my ConvertModel class, but I get this message: fatal error: unexpectedly found nil while unwrapping an Optional value – Andrew-Saarima Nov 19 '15 at 08:44

1 Answers1

0

So I don't know why or how UILabel lost it's initial link with the ViewController, but in an act of desperation I did the old Ctrl + Click drag over to ViewController and just named the resulting Outlet something else(tempOutputs), renamed the text assignment and now it works. So this isn't so much an answer as it is a work around for anyone who ends up here as well.

Andrew-Saarima
  • 270
  • 3
  • 9