Bug 28158 - Properties of type UIStringAttributes produce immutable objects that crash when you try to modify them
Summary: Properties of type UIStringAttributes produce immutable objects that crash wh...
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.6.0
Hardware: PC Mac OS
: --- enhancement
Target Milestone: Untriaged
Assignee: Alex Soto [MSFT]
Depends on:
Reported: 2015-03-18 12:35 UTC by Adam Kemp
Modified: 2015-03-26 09:43 UTC (History)
2 users (show)

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

Test case (10.38 KB, application/zip)
2015-03-18 12:35 UTC, Adam Kemp

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 Adam Kemp 2015-03-18 12:35:19 UTC
Created attachment 10400 [details]
Test case

There are various properties in UIKit objects that return a UIStringAttributes object (example: UINavigationBar.TitleTextAttributes). In UIKit these are dictionaries (NSDictionary), which by default are immutable. In the binding they are wrapped with a UIStringAttributes class, which assumes that they are mutable. This can lead to crashes.

To reproduce the issue run the attached project. You will crash on this line:

    titleAttribtues.ForegroundColor = UIColor.Red;

The exception is this:
    Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[__NSDictionaryI __setObject:forKey:]: unrecognized selector sent to instance 0x7b4984e0

The fix the crash you have to create a mutable version like this:

    titleAttribtues = new UIStringAttributes(new NSMutableDictionary(titleAttribtues.Dictionary));

This should be done automatically as needed inside UIStringAttributes.

To be clear, this should be fixed for ALL properties that return a UIStringAttributes object, not just the UINavigationBar.TitleTextAttributes property.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-03-18 12:48:51 UTC
Alex, can you have a look at this?
Comment 2 Alex Soto [MSFT] 2015-03-26 09:43:25 UTC
@Adam Thanks for the report!

Fixed in maccore master d00018dca293b829a000fb91779d75f8f2fb108e
@QA added test ^