Nathan McKaskle Nathan McKaskle - 7 months ago 25
Swift Question

Swift 2.1 New Way to Initialize String Array?

In my old Swift 1 code I could do this:

class MyArrayLists {

var myarray = [String, String]

init(){

myarray.append("First String", "Second String.")
}

}


Now after getting around to opening my code and trying to convert it to 2.1, it's telling me I can't do this anymore. Next to the declaration we see:


Expected member name or constructor call


Next to the append statement I see:


Cannot invoke append with argument of type (String, String)


What changed?

JAL JAL
Answer

With Swift 2 you explicitly have to declare the type of object in the array in parenthesis:

class MyArrayLists {

    var myarray: [(String, String)] = [(String, String)]()

    init(){

        self.myarray.append(("First String", "Second String."))
    }

}