Quentin Del
Quentin Del

Reputation: 1685

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
    }

Upvotes: 6

Views: 18233

Answers (3)

danielhadar
danielhadar

Reputation: 2161

If you're getting this error in testing (e.g. using LLReactiveMatchers's sendValues()), try to make your type (struct/class) conform to Equatable.

Upvotes: 0

Nirav D
Nirav D

Reputation: 72410

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

Upvotes: 1

Uncommon
Uncommon

Reputation: 3383

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
}

Upvotes: 5

Related Questions