Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot invoke 'value' with an argument list of type '(String)'

I am a beginner at Swift and I am migrating an app to Swift 3.0

I keep having this error and I have no idea how to solve it.

"Cannot invoke 'value' with an argument list of type '(String)'"

It is displayed at nearly each line of this snippet. Do you have any idea where it could come from? Thanks a lot

        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

            }

        }

As requested here is more information about Messages

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

Here is even more information

        // 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
    }
like image 813
Quentin Del Avatar asked Feb 07 '17 16:02

Quentin Del


2 Answers

You probably need to change them to (message as AnyObject).value(forKey:"···") (adding the forKey: label).

Do you know what kind of object message is supposed to be? Repeatedly casting it to AnyObject is odd. It would be cleaner to create a new variable - let messageObject = message as AnyObject and then call messageObject.value(forKey:"···"). (I suspect you really want to cast it to Dictionary or something like that, in which case you can do messageDictionary["···"] instead of calling value(forKey:).)

Also, in Swift you can do this to reduce redundancy even more:

if let content = messageObject.value(forKey:"content") as? String {
  stringContent = content
}
like image 175
Uncommon Avatar answered Oct 02 '22 18:10

Uncommon


If message is json dictionary then cast it to [String:Any] and then use subscript with it.

if let dic = message as? [String:Any] {
    stringContent = dic["content"] as? String ?? ""
}

OR

If message is type of Messages then you can access its property directly using message.content and same way you can access other properties too.

stringContent = message.content
like image 31
Nirav D Avatar answered Oct 02 '22 16:10

Nirav D