在 Swift 中更改 backgroundColor,Xcode 6
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27072547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing backgroundColor in Swift, Xcode 6
提问by Frank Barrett
I know the code to change the backgroundColor in iOS is something to this extent:
我知道在 iOS 中更改 backgroundColor 的代码是这样的:
self.view.backgroundColor = UIColor.yellowColor()
But for some reason, I am getting an "Unexpected declaration" error.
但出于某种原因,我收到了“意外声明”错误。
Anybody know why?
有人知道为什么吗?
This is the file I am dealing with, ViewController.swift. It is literally just a new single-view app template from Xcode 6.
这是我正在处理的文件 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.
}
}
The error is
错误是
/Users/frank/Dropbox/code/ios/backgroundColor/backgroundColor/ViewController.swift:13:5: Expected ?>declaration
/Users/frank/Dropbox/code/ios/backgroundColor/backgroundColor/ViewController.swift:13:5: 预期 ?> 声明
回答by Jeffery Thomas
You put your code in the wrong place.
你把代码放在错误的地方。
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.
}
}