Bug 30744 - Calling SetValueForKey on NSMutableUrlRequest.Headers crashes
Summary: Calling SetValueForKey on NSMutableUrlRequest.Headers crashes
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.10
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-06-03 15:02 UTC by dj_technohead
Modified: 2015-06-03 20:44 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 dj_technohead 2015-06-03 15:02:49 UTC
NSMutableUrlRequest mutableRequest = (NSMutableUrlRequest)request.MutableCopy ();
if (mutableRequest.Headers == null)
  mutableRequest.Headers = new NSMutableDictionary ();

// this crashes for some reason
mutableRequest.Headers.SetValueForKey(new NSString ("value"), new NSString ("Authorization"));
Comment 1 dj_technohead 2015-06-03 16:25:02 UTC
Found a workaround:

NSMutableUrlRequest mutableRequest = (NSMutableUrlRequest)request.MutableCopy ();

NSMutableDictionary mutableHeaders;

if (mutableRequest.Headers == null)
	mutableHeaders = new NSMutableDictionary ();
	mutableHeaders = (NSMutableDictionary)mutableRequest.Headers.MutableCopy();

mutableHeaders.SetValueForKey(new NSString ("value"), new NSString ("Authorization"));
Comment 2 Sebastien Pouliot 2015-06-03 20:44:22 UTC
It's not really a workaround, it's how it works (in ObjC). Xamarin.iOS bindings expose what the API does, nicer syntax but same native functionalities.

E.g. this following ObjC would print `NSDictionary` twice, iow `allHTTPHeaderFields` is always an `NSDictionary` to you (even if you assign it a mutable dictionary).

    NSURLRequest *r = [[NSURLRequest alloc] init];

    NSMutableURLRequest *mr = [r mutableCopy];
    mr.allHTTPHeaderFields = [[NSDictionary alloc] init];

    NSMutableURLRequest *mr2 = [mr mutableCopy];
    @try {
        [mr2.allHTTPHeaderFields setValue:@"Ok" forKey:@"Authorization"];
        NSLog (@"NSMutableDictionary");
    @catch (NSException *e) {
        NSLog (@"NSDictionary");
    NSMutableDictionary *md = [[NSMutableDictionary alloc] init];
    [md setValue:@"Fail" forKey:@"Authorization"];
    mr2.allHTTPHeaderFields = md;
    @try {
        [mr2.allHTTPHeaderFields setValue:@"Ok" forKey:@"Authorization"];
        NSLog (@"NSMutableDictionary");
    @catch (NSException *e) {
        NSLog (@"NSDictionary");
    return YES;

Mutability often seems simple but it can be treacherous too. There's a nice article about it @ https://www.bignerdranch.com/blog/about-mutability/