4

I'm working with this kind of array.

var myArray : [[String:String]] = [["1" : "Arun", "2" : "Gupta"],["1" : "John", "2" : "Cena"],["1" : "James" , "2" : "Bond"],["1" : "Iron", "2" : "Man"],["1" : "Karthik","2" : "Keyan"]]

I could assign values directly to this array and access the values through object. For example,

To access values in index zero.

let obj = myArray[0]
print(obj["1"],obj["2"])

it prints values in myArray[0]

Output:

Arun Gupta.

Now what i want is, how to append values to this kind of array instead of directly assigning values in the declaration itself.

7
  • 2
    you mean myArray.append(["1" : "Arun", "2" : "Gupta"]) Commented Jan 16, 2017 at 7:01
  • yes, and how this array must be declared ? 'var myArray : [[String:String]] ' is this right ? Commented Jan 16, 2017 at 7:22
  • 1
    you can just declare like var myArray = [[String:String]]() Commented Jan 16, 2017 at 7:22
  • It works only after including this '()' parentheses. Thanks!. i wonder why this kind of array requires this parentheses ? Commented Jan 16, 2017 at 7:34
  • 1
    @KarthikeyanSk You can also use var myArray: [[String: String]] = [] to initialize your array. Commented Jan 16, 2017 at 8:42

2 Answers 2

2

Try this :

var array1 = ["1" : "Arun", "2" : "Gupta"]

myArray.append(array1)
Sign up to request clarification or add additional context in comments.

1 Comment

Should the declaration be like this ' var myArray : [[String:String]] '
1

Try append()

var myArray = [[String:String]]()
myArray.append(["1" : "Arun", "2" : "Gupta"])
print(myArray)

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.