Set UIButton Image to Swift - currently expands to zero

I'm having a hard time setting the UIButton image. I've checked other posts on how to do this and it seems simple enough (but then, I'm here).

In my program, the image displayed on the button depends on I am using a switch to set the image before trying to set the button image. However, when debugging, the console displays the following:

'2015-05-03 13: 40: 31.117 PackPlanner [581: 4465] <UIImage: 0x7fd2a843fac0>, {600, 300} fatal error: unexpectedly found nil while expanding optional value (Lldb)'

class TripOverviewViewController: UITableViewController {

@IBOutlet var highTempLabel: UILabel!
@IBOutlet var lowTempLabel: UILabel!
@IBOutlet var weatherLabel: UILabel!
@IBOutlet var locationLabel: UILabel!
@IBOutlet var dateLabel: UILabel!
@IBOutlet var numOfPeopleLabel: UILabel!
@IBOutlet var sunriseLabel: UILabel!
@IBOutlet var sunsetLabel: UILabel!
@IBOutlet weak var weatherButtonImage: UIButton!

@IBAction func weatherButton(sender: UIButton) {

var tripItem: AnyObject? {
    didSet {
        // Update the view.

func configureView() {
    // Update the user interface for the trip detail.
            if let trip: Trip = self.tripItem as? Trip{

                if var label = self.locationLabel {
                    label.text = trip.location
                if var label = self.numOfPeopleLabel {
                    label.text = trip.numOfPeople.stringValue
                if var label = self.dateLabel {
                    var formattedDate = NSDateFormatter.localizedStringFromDate(trip.startDate, dateStyle: .LongStyle, timeStyle: .NoStyle)
                    label.text = formattedDate
                if var label = self.weatherLabel {
                    label.text =

                var theImage = UIImage(named: "Sunny") as UIImage!
                switch {
                    case "Partly Cloudy":
                        theImage = UIImage(named:"PartlyCloudy")
                    case "Light Snow":
                        theImage = UIImage(named:"LightSnow")
                    case "Rainy":
                        theImage = UIImage(named:"Rainy")
                        theImage = UIImage(named:"Sunny")

                NSLog(" \(theImage.description)") //for debugging
                self.weatherButtonImage.setImage(theImage, forState: .Normal)


override func viewDidLoad() {




source to share

2 answers

Try this after NSLog ("(theImage.description)")

    if let tempButton = self.weatherButtonImage
    tempButton.setImage(theImage, forState:.Normal)
    } else
    { NSLog("Button is nil")


To determine if its button is null.

If your button is null, reconnect it in the storyboard.



Are you sure the object exists and is not null? UIImage may not be able to find this image named "Sunny". Another possibility is that self.weatherButtonImage is zero at this point (I can see its output), so try setting it when the IB layout is complete in viewDidLayoutSubviews.



All Articles