Bug 17573 - NS(Mutable)AttributedString constructor throws when given an explicit null for an argument that has null as a default argument
Summary: NS(Mutable)AttributedString constructor throws when given an explicit null fo...
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.0.6.x
Hardware: PC Mac OS
: Normal normal
Target Milestone: 7.2.1
Assignee: Bugzilla
Depends on:
Reported: 2014-02-03 12:19 UTC by Adam Kemp
Modified: 2014-03-20 09:30 UTC (History)
4 users (show)

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

Example project (7.99 KB, application/zip)
2014-02-03 12:19 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 2014-02-03 12:19:12 UTC
Created attachment 5962 [details]
Example project

I get an ArgumentNullException with this code:
var attributedString = new NSAttributedString("Hello, World!", foregroundColor: null);

That is unexpected because the default value for the foregroundColor (along with every other attribute argument) is null.

The reason I want this is that I am writing a method that does something like this:

private static void AppendStringToAttributedString(NSMutableAttributedString attributedString, string appendString, UIColor foregroundColor = null)
    using (var attributedAppendString = new NSAttributedString(appendString, foregroundColor: foregroundColor))

I want to be able to call it like this:
using (var attributedString = new NSMutableAttributedString())
    AppendStringToAttributedString("Hello, ");
    AppendStringToAttributedString("World!", foregroundColor: UIColor.Red);

The exception prevents me from doing this. :|

This should be fixed in both NSAttributedString and NSMutableAttributedString, and ideally anywhere else where this pattern exists.
Comment 1 Mohit Kheterpal 2014-02-03 12:43:22 UTC
After deploying attached sample on simulator we are getting same issue.

Exception Detail: https://gist.github.com/saurabh360/04e71a37998744db0750
IDE Log : https://gist.github.com/saurabh360/0d270b0c89a9fea74cef
Comment 2 Sebastien Pouliot 2014-02-05 20:25:08 UTC
The ObjC API uses an NSDictionary [1] and XI added a strongly-typed `UIStringAttributes` overload.

Internally this returns a NSDictionary (for consumption by the ObjC API) but can return `null` if no member is set (that code is used elsewhere).

Sadly [1] does not specify allowing null so there's no [NullAllowed] in the bindings. OTOH it likely does allow null (most similar API does, but I'll confirm/test that) and adding a [NullAllowed] would fix this (otherwise I'll fix the 2nd code part, which returns a null instead of an empty dictionary).

[1] https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSAttributedString_Class/Reference/Reference.html#//apple_ref/occ/instm/NSAttributedString/initWithString:attributes:
Comment 3 Sebastien Pouliot 2014-02-10 14:54:30 UTC
A nil NSDictionary is accepted by the ObjC API.

Fixed in master 83a12543a40bb24299ed45ed60fe01732b322e98

QA: unit tests added in the same revision
Comment 4 Ram Chandra 2014-03-20 09:30:21 UTC
I have checked this issue on following builds:

Mac 10.7.5
Xamarin Studio : 4.2.4 (build 32)
Xamarin.iOS : (Trial Edition)
Build Information
Release ID: 402040032
Git revision: a160c35dac9ab9fd32eeadaa171216316d5a5133
Build date: 2014-03-12 13:55:08-04
Xamarin addins: a779416ceabd54981ce812771a4061c942e8b872

I observe that now I am not getting an ArgumentNullException with the following code:
"var attributedString = new NSAttributedString("Hello, World!", foregroundColor:null);"
I have check this issue with attached project for "NSAttributedString" and "NSMutableAttributedString"  and these are working fine. 

screencast:  http://www.screencast.com/t/qRUOTSAo5ZiE

This issue is working fine.Hence, I am closing this issue.

Please let me know if I have to check anything else.