改变Swift中的背景颜色,Xcode 6

时间:2014-11-22 00:17:51

标签: ios xcode swift

我知道改变iOS中backgroundColor的代码就是这样:

self.view.backgroundColor = UIColor.yellowColor()

但出于某种原因,我收到了“意外声明”错误。

有人知道为什么吗?

这是我正在处理的文件,ViewController.swift。它实际上只是Xcode 6中的一个新的单视图应用程序模板。

//
//  ViewController.swift
//  backgroundColor
//
//  Created by Frank Barrett on 11/21/14.
//  Copyright (c) 2014 Frank Barrett. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

self.view.backgroundColor = UIColor.yellowColor()

override func viewDidLoad() {
    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.
}


}

错误是

  

/Users/frank/Dropbox/code/ios/backgroundColor/backgroundColor/ViewController.swift:13:5:预期?>声明

1 个答案:

答案 0 :(得分:6)

您将代码放在错误的位置。

class ViewController: UIViewController {

    // This space is only for declarations.

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // Put this setup code in the viewDidLoad method.
        self.view.backgroundColor = UIColor.yellowColor()
    }

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