-1

since Swift it is possible to create arrays with multiple values like

myArray = [ String : Boolean ]

i want to create an array like this out of a plist, but it seems not possible in the xcode-editor to create an array like this (you can only save one single value per array-index). any ideas?

ixany
  • 5,433
  • 9
  • 41
  • 65

2 Answers2

0

You can store any number of value in swift array.Just use

var myArray:[[AnyObject]] = []
myArray.append([1,2,"abc"])   //store at 0 index

The above example is array of AnyObject arrays so you can store any kind of value.You can make array of Dictionaries

In your code you are referring to Dictionary not array.

To store in plist you have to use NSDictionary refer this answer

Community
  • 1
  • 1
codester
  • 36,891
  • 10
  • 74
  • 72
0

Yes, you can save arrays with other collection types inside - other arrays, tuples, dictionaries, objects, etc. Just one example for a playground:

import Foundation

let a: [Int] = [1,2,3,4]
let b: [String] = ["a", "b", "c", "d"]

let c = [a, b]

println(c[0])

/*

(
1,
2,
3,
4
)

*/

println(c[1])

/*
(
a,
b,
c,
d
)

for your plist you could do this:

let a = [1,"a"]
let b = [2, "b"]

let c = [a, b]

println(c[0])

/*

(
1,
a
)

*/

println(c[1])

/*
(
(
2,
b
)
*/

if you use NSCoding, you prearrange all the data any way you want and get back in that form.

NSUserDefaults - How can I store an array of custom objects (Goals)

Community
  • 1
  • 1
Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53