Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add New Item To NSDictionary Doesn't Work

I use Xamarin.ios with the below code:

var dict = NSUserDefaults.StandardUserDefaults.DictionaryForKey("TheDictKey");
if (dict == null)
{
    dict = new NSDictionary();
    NSUserDefaults.StandardUserDefaults.SetValueForKey(dict,(NSString)"TheDictKey");
}
dateTime = dateTime.ToUniversalTime();
dict[(NSString)"ValueKey"] = new NSString("Value"); //<------ throw exception here

The last sentence alway throw exception "NotSupported".

How can I update the dictionary? Thanks

like image 913
frank Avatar asked Jan 30 '26 05:01

frank


1 Answers

That is because NSDictionary is immutable. That means that after it is created, you cannot change it. Use NSMutableDictionary for this purpose instead, which is a subclass of NSDictionary.

From Apple's NSDictionary reference:

NSDictionary creates static dictionaries, and NSMutableDictionary creates dynamic dictionaries.

Here: click

like image 149
Dimitris Tavlikos Avatar answered Feb 02 '26 04:02

Dimitris Tavlikos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!