在 ViewController 中创建数组的预期声明错误,不知道为什么 [英] Expected Declaration error creating array in ViewController, can't work out why

查看:19
本文介绍了在 ViewController 中创建数组的预期声明错误,不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我使用的是 Xcode 6 beta 2.其次,我确实有编程经验(基础、VB、脚本语言),但它不包括任何严重的 OO 编程,而且我对 IOS 编程完全陌生.直接进入 Swift.在此先感谢那些可以提供帮助的人.这几天我一直在为此苦苦挣扎.

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.

在构建简单的 UIImage 数组时遇到问题.(为了简单起见,我删除了所有其他代码.)我试图理解为什么在 viewDidLoad() 中声明 UIImage 数组和加载图像有效,但不是在 ViewController 的基础",这是我似乎需要的地方它用于其他工作.

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 变量.)

(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.)

这是我的代码:

//  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.

    }

}

推荐答案

您只能在类中的方法之外进行属性声明.类的所有功能都在方法内部.当您在方法之外声明 var icon = UIImage[]() 时,它是一个实例属性声明并且是有效代码.

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.

接下来的两行代码试图修改该属性.方法之外的代码永远不会执行,因为无法调用它.虽然您可以在方法之外声明属性,但您必须在类中的方法内使用它们.

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.

我建议您学习更多有关面向对象编程的知识,因为您似乎还没有完全掌握它.您可能想尝试一种比 swift 目前具有更多可靠性和学习资源的语言.如果您打算进行 iOS 开发,即使您想使用 Swift,学习 Objective-c 也会很有帮助,因为您将接触到 Apple 的 API,它们在两种语言中都是相同的.

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.

这篇关于在 ViewController 中创建数组的预期声明错误,不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆