xcode 在 ViewController 中创建数组的预期声明错误,不知道为什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24479700/
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
Expected Declaration error creating array in ViewController, can't work out why
提问by dave_the_dev
First, I am using Xcode 6 beta 2. Second, I do have programming experience (basic, VB, script languages) ,but it doesnt include any serious OO programming, and I am totally new to IOS programming. Going straight into Swift. In advance, thanks to those who can help. I have been struggling over this a few days now.
首先,我使用的是 Xcode 6 beta 2。其次,我确实有编程经验(基础、VB、脚本语言),但它不包括任何严重的 OO 编程,而且我对 IOS 编程完全陌生。直接进入 Swift。在此先感谢那些可以提供帮助的人。这几天我一直在为此苦苦挣扎。
Having trouble building a simple UIImage array. (I've stripped out all other code for simplicity.) I'm trying to understand why declaring an UIImage array and loading images works within viewDidLoad(), but not at the "base" of ViewController, which is where I seem to need it for other things to work.
构建简单的 UIImage 数组时遇到问题。(为了简单起见,我已经删除了所有其他代码。)我试图理解为什么在 viewDidLoad() 中声明 UIImage 数组和加载图像有效,而不是在 ViewController 的“基础”,这是我似乎需要的地方它用于其他工作。
(I've noticed it seems to be tied into the fact that this is an array declaration, which futhers my confusion. I can declare and assign simple UIImage variables in either location.)
(我注意到它似乎与这是一个数组声明这一事实有关,这进一步加深了我的困惑。我可以在任一位置声明和分配简单的 UIImage 变量。)
Here's my code:
这是我的代码:
// 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"))
}
But this code does not:
但这段代码没有:
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.
}
}
回答by Connor
You can only have property declarations outside of methods in a class. All functionality of the class goes inside of methods. When you declare var icon = UIImage[]()
outside of a method, it's an instance property declaration and is valid code.
您只能在类中的方法之外进行属性声明。类的所有功能都在方法内部。当您var icon = UIImage[]()
在方法之外声明时,它是一个实例属性声明并且是有效代码。
Your next two lines attempt to modify the property. Code outside of methods is never executed because there is no way to call it. While you can declare properties outside of methods, you have to use them inside a method in your class.
接下来的两行尝试修改该属性。方法之外的代码永远不会执行,因为无法调用它。虽然您可以在方法之外声明属性,但您必须在类的方法中使用它们。
I'd recommend learning more about Object Oriented programming, because it seems like you don't quite have the grasp of it yet. You may want to try a language that has more reliability and learning resources than swift currently does. If you are planning on doing iOS development, it would be helpful to learn objective-c even if you want to use Swift because you'll gain exposure to Apple's APIs which are the same in both languages.
我建议您学习更多关于面向对象编程的知识,因为您似乎还没有完全掌握它。您可能想尝试一种比 swift 目前具有更多可靠性和学习资源的语言。如果您打算进行 iOS 开发,即使您想使用 Swift,学习 Objective-c 也会很有帮助,因为您将接触到 Apple 的 API,它们在两种语言中都是相同的。