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

17,290

Solution 1

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
}

Solution 2

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
Share:
17,290
Quentin Del
Author by

Quentin Del

French IoT Lover.

Updated on June 21, 2022

Comments

  • Quentin Del
    Quentin Del about 2 years

    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
        }
    
  • Quentin Del
    Quentin Del over 7 years
    Adding "forKey" fixed the issue.
  • Quentin Del
    Quentin Del over 7 years
    When I try if ((message.content != nil){ stringContent = ((message as AnyObject).value(forKey:"content") as? String)! } it says Value of type 'NSFastEnumerationIterator.Element' (aka 'Any') has no member ‘content' and proposes to change to if (((message as AnyObject).content != nil)
  • Nirav D
    Nirav D over 7 years
    @QuentinDel You need to show how you are getting value in message means how you have initialized the message that will give us idea.
  • Quentin Del
    Quentin Del over 7 years
    I have updated my post with more information about message. The issue is fixed now but I am always eager to learn how to write better code. Thanks a lot for your help.
  • Nirav D
    Nirav D over 7 years
    @QuentinDel You still don't get it what I have ask is the var message that you have used here (message as AnyObject).value("content") != nil). I'm asking about that message variable How you have initailized it you haven't add that code. If it is dictionary then subscript will work no need to type cast it to Anyobject and no need to use valueForKey.