maz maz - 7 months ago 59
Swift Question

Casting String to [NSObject : AnyObject]

I need to convert a string to an NSObject : AnyObject type so that I can run it into my Parse cloud code however I am having issues as it says I cannot cover type String to an unrelated type [NSObject : AnyObject]. How can I convert this string to that. I need to keep it a string so that I can eventually retrieve the string from a text field within my app. Below is my code

var name : String
name = "kooshesh"
let parameters : [NSObject : AnyObject]
parameters = (name as? [NSObject : AnyObject])!

PFCloud.callFunctionInBackground("AddFriendRequest", withParameters: parameters) { results, error in
if error != nil {
// Your error handling here
} else {


Because var name : String is a String, and let parameters : [NSObject : AnyObject] is a dictionary. These are unrelated types that cannot be converted from one to the other.

More than likely, you should have a key for the parameters value so you can do something like this:

parameters = [ "MyKey" : name]