xcode 无法使用 StringLiteralConvertible 类型的参数列表调用“init”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27792514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:27:24  来源:igfitidea点击:

Cannot invoke 'init' with argument list of type StringLiteralConvertible

iosxcodeswift

提问by 4aRk Kn1gh7

when I try to run the code below I get this error:

当我尝试运行下面的代码时,出现此错误:

Cannot invoke 'init' with argument list of type (id:StringLiteralConvertible,host:Contact,target:Contact,text:StringLiteralConvertible)

I tried using NSString and String in my class but still having the same problem. Can anyone guide me what i'm missing here?

我尝试在我的班级中使用 NSString 和 String 但仍然遇到同样的问题。任何人都可以指导我在这里缺少什么吗?

class Contact {
    let id:String? = ""
    let name:String? = ""
    var number:String? = ""
    var photoPath:String? = ""
    var onlineStatus:Bool? = false

    init(id:String,name:String) {
        self.id = id
        self.name = name
        self.number = ""
        self.photoPath = ""
        self.onlineStatus? = false
    }
}

class Message {
    var id:String?
    var time:NSDate?
    var host:Contact?
    var target:Contact?

    init(id:String,host:Contact,target:Contact,time:NSDate) {
        self.id = id
        self.host = host
        self.target = target
        self.time = time
    }
}

class TextMessage:Message {
    var text:String?

    init(id: String, host: Contact, target: Contact, time: NSDate, text: String) {
        super.init(id: id, host: host, target: target, time: time)
        self.text = text
    }
}

let host = Contact(id: "", name: "")
let target = Contact(id: "", name: "")
let msg = TextMessage(id: "", host: host, target: target, time: NSData(), text: "")

回答by appleHyman42

You've got the wrong data type in your call to TextMessage init, should be NSDate()

您在调用 TextMessage init 时得到了错误的数据类型,应该是 NSDate()

let msg = TextMessage(id: "", host: host, target: target, time: NSDate(), text: "")