gpt4 book ai didi

swift - 尝试调用 convenience init, Swift 时调用中的额外参数

转载 作者:搜寻专家 更新时间:2023-10-31 08:19:14 26 4
gpt4 key购买 nike

我有简单的类(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
}
}

当我尝试使用 5 个参数测试第二个构造函数时,我得到: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

为什么?我错过了什么吗?

谢谢,

最佳答案

基于您的方法签名:

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

relationshipStatus 应该是 String 而不是 Int:

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

由于您没有为 relationshipStatus 传递正确的类型,编译器无法为您方便的 init 匹配方法签名,并回退到默认的 init(它能找到的最接近的匹配)这会触发 Extra argument 错误。

关于swift - 尝试调用 convenience init, Swift 时调用中的额外参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25270106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com