iphaaw
iphaaw

Reputation: 7204

How to initialise an empty dictionary that contain objects in Swift

I'm trying to set up a class that has a dictionary with an int and my own object. On init I do not have anything to put in this dictionary.

class Menu
{
    var emptyDic = Dictionary<String, String>()
    var menuItem: Dictionary<Int,MenuItem>()

    init()
    {
       //init object code
    }
}

The emptyDic variable (which I got from StackOverflow) works fine, but I get an error consecutive declarations on a line must be separated by a ; if I mirror the same syntax for my menuItem dictionary. If I remove the () it complains that self.menuItem is not initialised.

I've not seen much written about dictionaries with other types other than strings. Is this a case for making it an optional or am I missing something more obvious?

Thanks

Andrew

Upvotes: 0

Views: 221

Answers (1)

David Berry
David Berry

Reputation: 41226

You didn't copy the syntax. ':' and '=' are not equivalent. In this case, one specifies a type while the other specifies initialization.

Try:

var menuItem = Dictionary<Int,MenuItem>()

Upvotes: 2

Related Questions