Joel Derfner
Joel Derfner

Reputation: 2207

creating an array of dictionaries from a plist in swift

I'm very new to Swift (and can do basic Objective C but not much more than that). I have a plist of dictionaries and am trying to load it into an array. Here's my code:

            let resultArray = NSArray(contentsOfFile: bundlePath)!
            var swiftArray = [Dictionary<String,String>]()
            for (var i = 0; i < resultArray.count; i++){
                swiftArray.addObject(resultArray.objectAtIndex(i))
            }

I know that resultArray is created correctly. The last line of this code, however, gives me the error [(Dictionary<String,String>)] does not have a member named 'addObject'.

How can I get these dictionaries loaded into swiftArray?

Thanks for your help, guys and gals.

Upvotes: 2

Views: 1606

Answers (3)

shuhradk
shuhradk

Reputation: 31

Using Swift: I used a plist to store my array of dictionaries, loaded it up using an NSMutableArray. I show a way to get to the data below in the for loop.

// globals
var myArray = NSMutableArray()
var SamplePath = NSString()

SamplePath = NSBundle.mainBundle().pathForResource("MyFile", ofType: "plist")!
myArray = NSMutableArray(contentsOfFile: SamplePath as String)!

var i = 0 
for myElement in myArray {
    println(myArray[i]["Title"]as! String, myArray[i]["start"]as! Int, myArray[i]["detail"]as! String)
    i++
}

I created a temporary struct to load the value for an individual Dictionary when I need to do extra manipulation but simple things can be written directly into the array element using the setValue function of NSMutableArray:

myArray[Index].setValue(b, forKey:"Title").

Upvotes: 1

GoZoner
GoZoner

Reputation: 70165

You should be able to exploit NSArray bridging with simply:

var swiftArray = NSArray(contentsOfFile: file) as! [Dictionary<String,String>] 

For example:

39> arNS
$R9: NSArray = "2 values" {
  [0] = 1 key/value pair {
    [0] = {
      0 = (instance_type = 0x0000000102002da0 )
      1 = (instance_type = 0x0000000000000003)
    }
  }
  [1] = 1 key/value pair {
    [0] = {
      0 = (instance_type = 0x00000001020060e0 )
      1 = (instance_type = 0x0000000000000003)
    }
  }
}

40> var arSW = arNS as! [[String:Int]] 
arSW: [[String : Int]] = 2 values {
  [0] = {
    [0] = {
      key = "abc"
      value = 1
    }
  }
  [1] = {
    [0] = {
      key = "xyz"
      value = 2
    }
  }
}

Note: I applaud you for getting away from NSArray. If you find yourself needing to deal with NSArray methods in Swift, you might consider adding Swift Array extensions to implement the NSArray method but with immediate conversion to Array.

Upvotes: 0

Cihan Tek
Cihan Tek

Reputation: 5409

Use the append method instead of addObject. Swift arrays don't have an addObject method.

swiftArray.append(resultArray.objectAtIndex(i) as Dictionary<String, String>)

Or simply convert the NSArray to a swift array

var swiftArray = resultArray as [AnyObject] as [Dictionary<String, String>]

Upvotes: 2

Related Questions