【发布时间】:2014-08-20 05:01:18
【问题描述】:
首先,我使用的是 Xcode 6 beta 2。其次,我确实有编程经验(基础、VB、脚本语言),但它不包括任何严肃的 OO 编程,而且我对 IOS 编程完全陌生。直接进入 Swift。在此先感谢那些可以提供帮助的人。这几天我一直在为此苦苦挣扎。
在构建简单的 UIImage 数组时遇到问题。 (为简单起见,我删除了所有其他代码。)我试图理解为什么声明 UIImage 数组和加载图像在 viewDidLoad() 中工作,但不是在 ViewController 的“基础”,这是我似乎需要的地方它可以让其他事情发挥作用。
(我注意到它似乎与这是一个数组声明这一事实有关,这使我更加困惑。我可以在任一位置声明和分配简单的 UIImage 变量。)
这是我的代码:
// ViewController.swift
import UIKit
class ViewController: UIViewController {
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.
}
var icon = UIImage[]()
icon.append(UIImage(named: "yes.png")) <<==== expected declaration error
icon.append(UIImage(named: "no.png"))
}
但是这段代码没有:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var icon = UIImage[]()
icon.append(UIImage(named: "yes.png")) <==== no error, and builds
icon.append(UIImage(named: "no.png"))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】:
-
我不了解 Swift,但我认为问题在于您试图在任何方法之外使用代码。
-
您在班级成员级别编写代码的意图是什么。应该只有属性和方法声明。也许您想将代码移动到初始化程序
标签: ios arrays xcode uiviewcontroller swift