Bug 38179 - Xamarin bound Foundation's NSMutableDictionary literals incorrectly.
Summary: Xamarin bound Foundation's NSMutableDictionary literals incorrectly.
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.4 (iOS 9.2)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-01-29 14:28 UTC by Andrew Polkinghorn
Modified: 2016-01-29 15:34 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Andrew Polkinghorn 2016-01-29 14:28:16 UTC

NSMutableDictionary literal subscripts are bounded to "setObject:ForKey:".

We have a Objective-C framework and discovered we were getting the following crash on a sample with our Xamarin bounded framework:
"Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: *** setObjectForKey: object cannot be nil (key: 0)"

When doing a little background research it looks like we were setting an nil object to a mutable dictionary for a valid key. However, this was done using literal subscripts so it didn't crash on the Obj-C side as I'm assuming as the object parameter has the nullable attribute.

In Xamarin, this causes a crash as it looks like when it detects a literal it calls "setObject:forKey:" which the object parameter has the attribute non-nullable. Below shows this in Obj-C code where object is nil & index is valid:

    // Does NOT crash
    dict[index] = object;
    // Does NOT crash as nullable attribute on object parameter
    [dict setObject:object forKeyedSubscript:index];
    // Crashes due to non-nullable attribute on object parameter
    [dict setObject:object forKey:index];

Potential Solutions:
Use "setObject:atIndexedSubscript:" instead of "setObject:ForKey:".

Other Info:

Xamarin Studio
Version 5.10.1 (build 6)

Version: (Business Edition)

Is this a know issue?

Kind regards,
Andrew Polkinghorn.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2016-01-29 15:33:13 UTC
According to Apple's documentation, setObject:forKeyedSubscript: is identical to setObject:forKey: "This method is identical to setObject:forKey:." [1]

However experimental evidence shows the documentation is wrong, setObject:forKeyedSubscript: will remove the entry from the dictionary if the object is null, instead of throwing an exception.

In any case this is not something we can change, since it would modify the behavior of existing code.

[1] https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/#//apple_ref/occ/instm/NSMutableDictionary/setObject:forKeyedSubscript: