0

I have a class called Controller that contains one Array property. Right now, my class is declared like that:

class Controller {
    var myArray: [AnyObject] 

    init(bool: Bool) {
        if bool == true {
            myArray = [10, 11, 12]
        } else {
            myArray = ["Yo", "Ya", "Yi"]
        }
    }
}

The problem that I have with this code is that myArray is still (of course) of type [AnyObject] after my class initialization. Thus, every time I need to get an object out of myArray, I have to cast its type (Int or String) just like this:

let controller = Controller(bool: false)
let str = controller.array[0] as String

I want to be able to write let str = controller.array[0] //str: String without having to cast the real type of the objects inside myArray. Is there a way to do so? Do I have to use lazy init, struct, generic types?

Here is a attempt in pseudo code:

class Controller {
    var myArray: Array<T> //Error: use of undeclared type 'T'

    init(bool: Bool) {
        if bool == true {
            myArray = [10, 11, 12] as [Int] //myArray: Array<Int>
        } else {
            myArray = ["Yo", "Ya", "Yi"] as [String] //myArray: Array<String>
        }
    }
}
1
  • 2
    Why does your array need to contain variant types at run time? Is this something you could design around? It seems very confusing, I would be thrown by this if I were to jump into the code base. Commented Aug 4, 2014 at 16:45

3 Answers 3

3

So as Oscar and Elijah pointed out (up votes to them), I am just trying to be a little more verbose here. You need to declare the generic, T, when you define the class.

This would mean you need to define what that generic's type is at initialization of the class.

class Foo<T> {
    var items = [T]()
}

let stringFoo = Foo<String>()

stringFoo.items.append("bar")
stringFoo.items[0] = "barbar"

stringFoo.items                 // ["barbar"]



let intFoo = Foo<Int>()

intFoo.items.append(1)
intFoo.items[0] = 11

intFoo.items                    // [11]

So in your case, rather than passing a Bool for the init method, just define the generic's type at initialization instead.

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

Comments

3
class Controller<T> //You missed that T, right?
{ 
    var myArray: Array<T>
    /* initialization stuff */
}

var c = Controller<String>()
c.myArray[0] = "Hey!"
let str = c.myArray[0] //String, eventually!

Comments

2
class Controller<T> {
    var array: Array<T> = []

    ...
}

1 Comment

You can't add a little dialog to this?

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.