Swift: use constant property to define another property

I am trying to declare a static size array. I would like the constant to determine the size of the array.

I am trying to do the following in Swift

class foo {
    let size = 10
    let myArray = [Int](count: size, repeatedValue: 0)
}

      

But this is not a mistake

'foo.Type' does not have a member named 'size'       

      

If I don't use a size constant, the compiler is happy with it, but that's not what I would like. And there is no #define feature that I am aware of.

let myArray = [Int](count: 10, repeatedValue: 0)

      

+3


source to share


4 answers


As of Swift 1.2, you can simply add static information before sizing by making it a class constant and as such defined before myArray is defined:

class foo {
    static let size = 10
    let myArray = [Int](count: size, repeatedValue: 0)
}

      



Remember, however, that using the size later in your code requires you to match it exactly foo.size

.

+1


source


Swift gives you a couple of ways to do this. The simplest and most in line with the style #define

you mention is to declare it size

as a global constant:

let FOOSIZE = 10

class Foo {
    let myArray = [Int](count: FOOSIZE, repeatedValue: 0)
}

      



Alternatively you can define myArray

as a lazy variable and use a closure to fill its value. By the time the closure is complete, you will be able to access self.size

:

class Foo {
    let size = 10
    lazy var myArray: [Int] = { [Int](count: self.size, repeatedValue: 0) }()
}

      

+3


source


self

Not available in swift until all properties of the class / structure have been initialized and the subclass initializer has been called (in the case of an inherited class).

In your case, you are initializing properties outside of the initializer, but that does not change the result: you cannot initialize a variable implicitly referencing self

(which you do when you access a property size

).

However, size

it looks like a constant, and therefore it is better to instantiate it once (as a static property) rather than creating it in every instance of the class. Swift does not support static properties of a class, but structs do, so the trick is to define an internal private struct containing the static immutable properties you may need:

class foo {
    private struct Static {
        static let size = 10
    }

    let myArray = [Int](count: Static.size, repeatedValue: 0)
}

      

+3


source


One way is to have it in a function. This worked in the playground:

import UIKit

class ViewController: UIViewController {

    var myArray = [Int]()

    func appendArray (#index: Int, value: Int) {
        myArray[index] = value
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        let size = 10
        myArray = [Int](count: size, repeatedValue: 0)
        appendArray(index: 3, value: 4)
        println(myArray)

    }
}

      

Ok, I used the ViewController because it was convenient, but not necessary. No problem declaring the array as the side of the function. I was still using the function to create the array and used a different t value to change the value.

0


source







All Articles