4

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.

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.

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

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.

    }

}
2
  • I don't know Swift but I think the issue is because you are attempting to have code outside of any method. Commented Jun 29, 2014 at 19:59
  • what is your intention to write code on member level of the class. there should be only property and method declarations. maybe you want to move the code to an initializer Commented Jun 29, 2014 at 20:02

1 Answer 1

10

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.

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I appreciate your assistance!
Appreciate your assistance! what was/is throwing me (and still is a bit) is that I can var dice1 = UIImage(named: "Die-1.png") outside the methods, but seemingly not so with an array. As to obj-C .. tried and abandoned a few times... but I may need time for swift to mature and be more bug free so i can be sure an error is me and not Xcode/swift bugs.
That's because it is also a declaration. Variable declarations are allowed outside of methods. var a = 10 let b = "hello" etc
Variable declarations are allowed outside of methods but property declarations must occur inside a method. Is that right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.