Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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,

like image 724
snaggs Avatar asked Feb 13 '23 06:02

snaggs


2 Answers

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.

like image 53
Mike S Avatar answered Feb 14 '23 21:02

Mike S


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.

like image 44
Atomix Avatar answered Feb 14 '23 20:02

Atomix