Quentin Del Quentin Del - 1 year ago 56
Swift Question

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
}

Answer Source

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
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download