1

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?

3
  • what is my array supposed to be? An array of strings, an array of a 2-string-tuples? Commented Nov 23, 2015 at 19:58
  • Hey, it seems like you want to use a Dictionary, in that case replace var myarray = [String, String] to var myarray = [String: String]() To add you could use myarray["DictID"] = "DictValue" Commented Nov 23, 2015 at 20:01
  • No I need two strings in an array, it's not a key and value sort of thing it's two values. Commented Nov 23, 2015 at 20:08

2 Answers 2

2

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."))
    }

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

Comments

2

The compiler used to infer that

[String, String]

was actually an array of tuples:

[(String, String)]

and that appending a pair of values with

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

was sytactic sugar for what actually was happening behind the scene:

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

But now this automatic conversion has been deprecated.

The solution is to declare the proper type, and to append a proper tuple:

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

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

Comments

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.