Skip to content

Instantly share code, notes, and snippets.

@bmalbuck
Created January 14, 2015 01:20
Show Gist options
  • Select an option

  • Save bmalbuck/f0ec2f891233d48e332a to your computer and use it in GitHub Desktop.

Select an option

Save bmalbuck/f0ec2f891233d48e332a to your computer and use it in GitHub Desktop.
Swift array of dictionaries
import UIKit
/*
declare arrayOfDict with full type declaration else
bad things happen :/
ex:
do this
var arrayOfDict:Array<Dictionary <String, Any>> = ...
not this
var arrayOfDict = ...
*/
var arrayOfDict:Array<Dictionary <String, Any>> =
[["name": "Fred", "age": 35, "occupation": "teacher"],
["name": "Frank", "age": 42, "occupation": "doctor"],
["name": "Fanny", "age": 29, "occupation": "lawyer"],
["name": "Florence", "age": 57, "occupation": "chef"],
["name": "Fifi", "age": 20, "occupation": "waitress"],
["name": "Flan", "age": 50, "occupation": "accountant"],
["name": "Felix", "age": 33, "occupation": "engineer"],
["name": "Farah", "age": 41, "occupation": "pilot"]]
for element in arrayOfDict
{
var nameStr = element["name"] as String
let ageStr = String(element["age"] as Int)
let jobStr = element["occupation"] as String
nameStr += " is a "
nameStr += ageStr
nameStr += " year old "
nameStr += jobStr
println(nameStr)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment