无法将'String?.Type'类型的值转换为预期的参数类型'String?'

时间:2018-04-02 05:40:35

标签: ios swift string initialization

我收到标题中声明var updater的错误。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? ProductViewController,
            let index = tableViewProducts.indexPathForSelectedRow?.row {

            // Check whether postData array count is greater than index
            let updaterId = postData.count > index ? postData[index] : ""

            // Initialize "productsList" instance and assign the id value and sent this object to next view controller
            var updater = productsList(id: String?, p_name: String?, image: String?, audio: String?)
            updater.id = updaterId
            destination.updater = updater

这是我的productsList VC:

导入基金会

class productsList {

    let id: String?
    let p_name: String?
    let image: String?
    let audio: String?

    init(id: String?, p_name: String?, image: String?, audio: String?) {
        self.id = id
        self.p_name = p_name
        self.image = image
        self.audio = audio
    }

}

3 个答案:

答案 0 :(得分:0)

您必须将字符串值(或pgnuplot)作为参数而不是类型nil传递,这是错误所说的内容。

替换

String?

var updater = productsList(id: String?, p_name: String?, image: String?, audio: String?)
updater.id = updaterId

并且请遵守类名以大写字母开头的命名约定。

答案 1 :(得分:0)

此行导致问题

var updater = productsList(id: String?, p_name: String?, image: String?, audio: String?)

当你初始化你的对象时,即" ProductList"你必须传递你文件的实际值" id"," p_name"," image"和"音频"。

现在你要发送" String?" ,这是你的函数期望值的参数类型。

所以改变它就像

var updater = productsList(id: ID_OF_PRODUCT, p_name: NAMEOFPRODUCT, image: URLTOIMAGE/IMAGE, audio: AUDIOURL)

答案 2 :(得分:0)

使用此

var updater = productsList(id: updaterId, p_name: nil, image: nil, audio: nil)

而不是

var updater = productsList(id: String?, p_name: String?, image: String?, audio: String?)