Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
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
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.
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];
Use "setObject:atIndexedSubscript:" instead of "setObject:ForKey:".
Version 5.10.1 (build 6)
Version: 22.214.171.124 (Business Edition)
Is this a know issue?
According to Apple's documentation, setObject:forKeyedSubscript: is identical to setObject:forKey: "This method is identical to setObject:forKey:." 
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.