简体   繁体   中英

NSMutableString stringWithString Crashes the App

This is the code i have written to display content in a label.

UILabel *content;
content = [[UILabel alloc] initWithFrame:CGRectMake(8, loaderView.frame.size.height+loaderView.frame.origin.y+15, screenBounds.size.width-16, 10)];

NSMutableString *articleContent = [NSMutableString stringWithString:[articleDictionary objectForKey:@"content"]];

The app crashes while executing the line stringWithString .I checked lot of posts related to NSMutableString crash. But i am newbie and trying to understand memory allocation. should i check for content if nil ,or is there any memory leak, what am i doing wrong. I am using xcode 6.3 , ios 8.4

the content is NSString and it has value. How to rewrite the code checking for nil

This is the exception i am getting.

0   CoreFoundation 0x0000000183c3cf48 __exceptionPreprocess + 120
1   libobjc.A.dylib 0x00000001986fff80 objc_exception_throw + 52
2   CoreFoundation 0x0000000183c3ce90 +[NSException raise:format:] + 116
3   Foundation 0x0000000184ab9680 -[NSPlaceholderMutableString initWithString:] + 108
4   Foundation 0x0000000184ab9764 +[NSString stringWithString:] + 44
5   MyProject 0x0000000100104da8 -[ArticleDetailPage renderPage] (ArticleDetailPage.m:599)
6   MyProject 0x000000010010469c -[ArticleDetailPage viewWillAppear:] (ArticleDetailPage.m:556)
7   UIKit 0x00000001891e85f4 -[UIViewController _setViewAppearState:isAnimating:] + 624
8   UIKit 0x00000001891e8368 -[UIViewController __viewWillAppear:] + 152
9   UIKit 0x0000000189381fb4 -[UINavigationController _startCustomTransition:] + 1048
10  UIKit 0x000000018928e190 -[UINavigationController _startDeferredTransitionIfNeeded:] + 684
11  UIKit 0x000000018928de6c -[UINavigationController __viewWillLayoutSubviews] + 56
12  UIKit 0x000000018928ddd4 -[UILayoutContainerView layoutSubviews] + 204
13  UIKit 0x00000001891cb7ac -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 640
14  QuartzCore 0x00000001889cab58 -[CALayer layoutSublayers] + 144
15  QuartzCore 0x00000001889c5764 CA::Layer::layout_if_needed() + 288
16  QuartzCore 0x00000001889c5624 CA::Layer::layout_and_display_if_needed() + 28
17  QuartzCore 0x00000001889c4cc0 CA::Context::commit_transaction() + 248
18  QuartzCore 0x00000001889c4a08 CA::Transaction::commit() + 508
19  UIKit 0x00000001891c19d8 _afterCACommitHandler + 176
20  CoreFoundation 0x0000000183bf3bd0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28
21  CoreFoundation 0x0000000183bf1974 __CFRunLoopDoObservers + 368
22  CoreFoundation 0x0000000183bf1da4 __CFRunLoopRun + 924
23  CoreFoundation 0x0000000183b20ca0 CFRunLoopRunSpecific + 380
24  GraphicsServices 0x000000018ed5c088 GSEventRunModal + 176
25  UIKit 0x0000000189238ffc UIApplicationMain + 200
26  MyProject 0x00000001000ee7b4 main (main.m:15)
27  libdyld.dylib 0x0000000198f4e8b8 start + 0

Try this instead:

NSMutableString *articleContent = [articleDictionary[@"content"] mutableCopy];

If it's not set in the dictionary it will be nil and it avoids the potential crash in [NSMutableString stringWithString] (calling mutableCopy on nil is silently ignored).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM