2

I'm trying to create some sort of initWithDictionary initializer for one of my model in Swift. This is what I'm trying to do:

class CityModel: NSObject {

    var country: String
    var mapLat: String
    var mapLng: String
    var mapZoom: Int
    var name: String
    var nameShort: String
    var timezone: String
    var token: String

    init(country: String, mapLat: String, mapLng: String, mapZoom: Int, name: String, nameShort: String, timezone: String, token: String) {
        self.country = country
        self.mapLat = mapLat
        self.mapLng = mapLng
        self.mapZoom = mapZoom
        self.name = name
        self.nameShort = nameShort
        self.timezone = timezone
        self.token = token
    }

    convenience init(dict: Dictionary<String, AnyObject>) {
        self.init(
            country: (dict["country"] as String),
            mapLat: (dict["mapLat"] as String), // error
            mapLng: (dict["mapLng"] as String),
            mapZoom: (dict["mapZoom"] as Int),
            name: (dict["name"] as String),
            nameShort: (dict["nameShort"] as String),
            timezone: (dict["timezone"] as String),
            token: (dict["token"] as String)
        )
    }
}

I'm getting the following error:

CityModel.swift:34:18: Extra argument 'mapLat' in call

And I'm afraid I have no idea what I'm doing wrong. Any idea?

1 Answer 1

3

Change your dict declaration to:

dict: Dictionary<String, Any>

It's because you're using a mix of types including non-class types.

More information in Swift docs here: https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-XID_505

That "Extra argument" error message is a red-herring. I've found that it's usually complaining about the type of an argument not matching.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.