Andre V
Andre V

Reputation: 219

How to put an array of array into another array in Swift 1.2

var dic: [String: [[Item]]] //dic with string key and value of array in array of my Class Object

How can i take the values from this dic and store it in an array as this:

var array: [[Item]]//Array of array

How can I store the values from dic Into this array I tried using the for(key, value) statement. But it wouldn't let me append the values to the array variable. If you need more information I'm happy to give it, but if you understand what I'm trying to do and you know how to do it I appreciate your answer and it's very much needed!!!

Upvotes: 0

Views: 1669

Answers (2)

saurabh
saurabh

Reputation: 6775

If you want to get all the values in dictionary use dictionary.values, it will return you an array of all the values.

 var array = dic.values

If you want to go through each value in the dictionary use the following:

for value in dic.values {
   // println("Value: \(value)")
   array.append(value)
}

Upvotes: 1

Chirag Shah
Chirag Shah

Reputation: 3016

For your first line i give you suggestion that you should create your dictionary by

var array = ["123","456","789"]
var array1 = NSMutableArray()
array1 .addObject(array)
var dict = ["String":array1]
var array3 :NSArray  = dict["String"]!
println("value ::\(array3[0])")

Output that you get is:: "value ::(\n 123,\n 456,\n 789\n)"

May this help you

Upvotes: 0

Related Questions