Why can't I use SetValue for a dictionary?

Hello everyone. I am new to swift and declare a dictionary in my application as follows:

var imageDict : Dictionary<String, String> = [:] 

and I want to set the values ​​for this dictionary as follows:

 imageDict.setValue(NSStringFromCGPoint(frame), forKey: NSString.stringWithString("/(tagValue)")); 

But I got an error:

 Dictonary <String, String> does not have a member named 'setValue' 

This question is related to my previous question and may explain why I cannot set a value for this dictionary and can someone tell me another way to do this?

Thanks in advance.

+7
swift xcode6
source share
3 answers

Swift dictionary has no method like setValue: forKey :. Only NSMutableDictionary has such methods. If you want to assign a value to a key in swift, you must use subscriptip. Here is the right way to do it if you want to do it with a quick dictionary.

 var imageDict: Dictionary<String, String> = [:] imageDict["\(tagValue)"] = NSStringFromCGRect(frame) 

Or, if you want to use NSMutableDictionary, then it looks like this:

 var imageDict = NSMutableDictionary() imageDict.setObject(NSStringFromCGRect(frame), forKey: "\(tagValue)") 
+11
source share

I think you need to use NSMutableDictionary.

0
source share

You can use NSMutableDictionary like this!

  var emptyDictionary = NSMutableDictionary() emptyDictionary.setObject("Your_Value", forKey:"Your_key") print(emptyDictionary) 
-3
source share

All Articles