title: Programmatically set image to UIImageView with Xcode 6.1/Swift
body: <p>I'm trying to set UIImageView programmatically in Xcode 6.1:</p>
  
  <pre><code>@IBOutlet weak var bgImage: UIImageView!
  
  var image : UIImage = UIImage(named:"afternoon")!
  bgImage = UIImageView(image: image)
  bgImage.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
  view.addSubview(bgImage)
  </code></pre>
  
  <p>But Xcode is saying "expected declaration" with <code>bgImage = UIImageView(image: image)</code>  Image <code>"afternoon"</code>is a PNG, and my understanding is PNG does not need an extension in XCode 6.1.</p>
  
  <p>Also tried just <code>bgImage.image = UIImage(named: "afternoon")</code>, but still get:</p>
  
  <p><img src="https://i.stack.imgur.com/zslbH.jpg" alt="enter image description here"></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>OK, I have put the code to update <code>UIImageView</code> into the <code>viewDidLoad</code> function, but <code>UIImageView</code> is still not showing the image (which exists in the base directory as afternoon.png):</p>
  
  <pre><code>@IBOutlet weak var bgImage: UIImageView!
  @IBOutlet weak var dateLabel: UILabel!
  @IBOutlet weak var timeLabel: UILabel!
  
  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
  
      updateTime()
      var timer = NSTimer()
      let aSelector : Selector = "updateTime"
      timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
  
      var image : UIImage = UIImage(named:"afternoon")!
      bgImage = UIImageView(image: image)
  }
  </code></pre>