当前位置 : 主页 > 手机开发 > 其它 >

swift – 无法使用类型'(String)’的参数列表调用’value’

来源:互联网 收集:自由互联 发布时间:2021-06-11
我是 Swift的初学者,我正在将应用程序迁移到Swift 3.0 我一直有这个错误,我不知道如何解决它. “无法使用'(String)’类型的参数列表调用’value’” 它几乎显示在此代码段的每一行. 你知道
我是 Swift的初学者,我正在将应用程序迁移到Swift 3.0

我一直有这个错误,我不知道如何解决它.

“无法使用'(String)’类型的参数列表调用’value’”

它几乎显示在此代码段的每一行.
你知道它可能来自哪里吗?
非常感谢

if ((message as AnyObject).value("content") != nil){
            stringContent  = (message as AnyObject).value("content") as? String
        }

        if ((message as AnyObject).value("sender_uuid") != nil){
            stringSenderUuid  = (message as AnyObject).value("sender_uuid") as? String
        }

        if ((message as AnyObject).value("recipient_uuid") != nil){
            stringRecipientUuid  = (message as AnyObject).value("recipient_uuid") as! String
        }

        if ((message as AnyObject).value("date") != nil){

            if let result_number = (message as AnyObject).value("date") as? NSNumber
            {
                stringDate = "\(result_number)"
            }
            else   {

                stringDate  = (message as AnyObject).value("date") as! String

            }

        }

根据要求,这里有关于消息的更多信息

class Messages: Object {

dynamic var channel_name = ""
dynamic var content = ""
dynamic var sender_uuid = ""
dynamic var recipient_uuid = ""
dynamic var date = ""
dynamic var message_uuid = ""


override class func primaryKey() -> String? {
    return "message_uuid"
}    }



    let message = Messages()
    message.channel_name=channel_name
    message.content=stringContent
    message.sender_uuid = stringSenderUuid
    message.recipient_uuid = stringRecipientUuid
    message.date = stringDate
    message.message_uuid = stringUuid

这里有更多信息

// initialize with nothing
    var stringContent = " "
    var stringSenderUuid = " "
    var stringRecipientUuid = " "
    var stringDate = " "
    var stringUuid = " "


    // We check for existence in the dictionnary
    if (message["content"] != nil){
         stringContent  = message["content"] as! String
    }

    if (message["sender_uuid"] != nil){
         stringSenderUuid  = message["sender_uuid"] as! String
    }

    if (message["recipient_uuid"] != nil){
         stringRecipientUuid  = message["recipient_uuid"] as! String
    }

    if (message["date"] != nil){
        if let result_number = message.value(forKey: "date") as? NSNumber
        {
            stringDate = "\(result_number)"
        }
        else   {

            stringDate  = message.value(forKey: "date") as! String

        }
    }
    if (message["uuid"] != nil){
         stringUuid  = message["uuid"] as! String
    }
您可能需要将它们更改为(消息为AnyObject).value(forKey:“···”)(添加forKey:标签).

你知道应该是什么样的对象消息吗?反复将它转换为AnyObject是奇怪的.创建一个新变量会更简洁 – 让messageObject = message作为AnyObject,然后调用messageObject.value(forKey:“···”). (我怀疑你真的想把它转换成Dictionary或类似的东西,在这种情况下你可以做messageDictionary [“···”]而不是调用value(forKey :).)

此外,在Swift中,您可以执行此操作以进一步减少冗余:

if let content = messageObject.value(forKey:"content") as? String {
  stringContent = content
}
网友评论