snaggs
snaggs

Reputation: 5713

Extra argument in call when try to call convenience init, Swift

I have simple class:

class WmAttendee{

    var mEmail:String!
    var mName:String!
    var mType:Int!
    var mStatus:String = "0"
    var mRelationShip:String!

    init( email:String, name:String, type:Int) {
        self.mEmail = email
        self.mName = name
        self.mType = type
    }

     convenience init( email:String,  name:String,  type:Int,  status:String, relationShip:String) {
        self.init(email: email, name: name, type: type)
        self.mStatus = status
        self.mRelationShip = relationShip
    }
}

When I try to test 2nd constructor with 5 parameters, I get: Extra argument 'status' in call

var att1 = WmAttendee(email: "myMail", name: "SomeName", type: 1); // OK

var att2 = WmAttendee(email: "mail2", name: "name2", type: 3, status: "2", relationShip: 3)
 // ERROR Extra argument 'status' in call

Why? Do I miss something?

Thanks,

Upvotes: 4

Views: 4778

Answers (2)

Mike S
Mike S

Reputation: 42325

Based on your method signature:

convenience init( email:String,  name:String,  type:Int,  status:String, relationShip:String)

relationshipStatus should be a String and not an Int:

var att2 = WmAttendee(email: "mail2", name: "name2", type: 3, status: "2", relationShip: "3")

Since you're not passing the correct type for relationshipStatus, the compiler can't match the method signature for your convenience init and falls back the the default init (the closest match it can find) which triggers the Extra argument error.

Upvotes: 7

Atomix
Atomix

Reputation: 13842

Your are passing a parameter of the wrong type to your function. 'RelationShip' must be of type String, but you are passing an Integer. Yes, the compiler error is misleading, but then again swift is still in beta.

Upvotes: 1

Related Questions