Reputation: 720
I currently have this:
var locations = [
["location": "New York", "temp": "2 °C", "wind": "3 m/s"]
]
And I add stuff to this with locations.append()
. It works great!
However, I don't want there to be a default entry. So I tried
var locations = [] // Not working.
var locations = [] as NSArray
var locations = [] as NSMutableArray
var locations = [] as NSDictionary
var locations = [] as NSMutableDictionary
var locations = [:] as ... everything
var locations = [APIData]
Feels like I've tried everything but I still get countless errors whatever I try. At this stage I'm even surprised my default locations is working.
How do I solve this? How do I make locations empty to start with?
Upvotes: 0
Views: 733
Reputation: 589
If we assume you try to initialize a array of dictionary with key String
and value String
you should:
var locations: [[String: String]] = []
then you could do:
locations.append(["location": "New York", "temp": "2 °C", "wind": "3 m/s"])
Upvotes: 4
Reputation: 3020
To create an array or dictionary you should know the type you need.
With
var array: [String]
you declare a variable to contain an array of strings. To initialize it, you can use
var array: [String] = [String]()
You can omit the : [String]
because the compiler can automatically detect the type in this case.
To create a dictionary you can do the same, but you need to define both, the key and the value type:
var dictionary = [String: String]()
Now, what you ask for is an array of dictionaries, so you need to combine both to
var arrayOfDictionaries = [[String: String]]()
Upvotes: 0