Return an NSMutableString as NSString Avoiding “Uncaught Error 11″ with Cocoa

×

This post was originally published in 2009
It may contain stale & outdated information. Or it may have grown more awesome with age, like the author.

Another stumbling block on the road to Slider completion was this:

NSUncaughtSystemExceptionException — Uncaught system exception: signal 11

This vague and unhelpful error message (in this case) was caused by my trying to return an NSMutableString in place of an NSString:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (NSString *)generateHTML{
 
	NSMutableString *html = [NSMutableString string];
 
	@try{
 
	//Begin wrapper
	[html appendString:[NSString stringWithFormat:@"<div id=\"slider%@\" style=\"height:%ipx;width:%ipx;position:relative;z-index:0;margin-left:%ipx;%@\">",
						UID, (int)portalSize.height, (int)portalSize.width, leftMargin, [self generateBGCSS]]];
 
	//Begin other content
	[html appendString:[self someYetToBeWrittenFunction]];
 
	//Close wrapper
	[html appendString:@"</div>"];
 
	}
	@catch (NSException *e) {
		NSLog(@"generateHTML: %@", e);
	}
	return html;
}

Googling resulted in this solution, from the Cocoa Dev Archives (which have saved me from suicide more times than I can count).

The successful code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (NSString *)generateHTML{
 
	NSMutableString *html = [NSMutableString string];
 
	@try{
 
	//Begin wrapper
	[html appendString:[NSString stringWithFormat:@"<div id=\"slider%@\" style=\"height:%ipx;width:%ipx;position:relative;z-index:0;margin-left:%ipx;%@\">",
						UID, (int)portalSize.height, (int)portalSize.width, leftMargin, [self generateBGCSS]]];
 
	//Begin other content
	[html appendString:[self someYetToBeWrittenFunction]];
 
	//Close wrapper
	[html appendString:@"</div>"];
 
	}
	@catch (NSException *e) {
		NSLog(@"generateHTML: %@", e);
	}
	return [NSString stringWithString:html];
}

This should have been obvious, I regret the shame I bring on my family.

No comments | Trackback