【问题标题】:NSMutableString stringWithString Crashes the AppNSMutableString stringWithString 使应用程序崩溃
【发布时间】:2016-03-05 17:53:19
【问题描述】:

这是我编写的用于在标签中显示内容的代码。

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"]];

应用程序在执行stringWithString 行时崩溃。我查看了很多与NSMutableString 崩溃相关的帖子。但我是新手,试图了解内存分配。 如果nil,我应该检查内容,还是有任何内存泄漏,我做错了什么。 我正在使用xcode 6.3ios 8.4

内容是 NSString 并且它具有价值。如何重写代码检查nil

这是我得到的例外。

0 核心基础 0x0000000183c3cf48 __exceptionPreprocess + 120 1 libobjc.A.dylib 0x00000001986fff80 objc_exception_throw + 52 2 核心基础 0x0000000183c3ce90 +[NSException raise:format:] + 116 3 基础 0x0000000184ab9680 -[NSPlaceholderMutableString initWithString:] + 108 4 基础 0x0000000184ab9764 +[NSString stringWithString:] + 44 5 我的项目 0x0000000100104da8 -[ArticleDetailPage 渲染页面] (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 石英核心 0x00000001889c5764 CA::Layer::layout_if_needed() + 288 16 石英核心 0x00000001889c5624 CA::Layer::layout_and_display_if_needed() + 28 17 石英核心 0x00000001889c4cc0 CA::Context::commit_transaction() + 248 18 石英核心 0x00000001889c4a08 CA::Transaction::commit() + 508 19 UIKit 0x00000001891c19d8 _afterCACommitHandler + 176 20 核心基础 0x0000000183bf3bd0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28 21 核心基础 0x0000000183bf1974 __CFRunLoopDoObservers + 368 22 核心基础 0x0000000183bf1da4 __CFRunLoopRun + 924 23 核心基础 0x0000000183b20ca0 CFRunLoopRunSpecific + 380 24 图形服务 0x000000018ed5c088 GSEventRunModal + 176 25 UIKit 0x0000000189238ffc UIApplicationMain + 200 26 我的项目 0x00000001000ee7b4 主要 (main.m:15) 27 libdyld.dylib 0x0000000198f4e8b8 开始 + 0

【问题讨论】:

  • 崩溃原因是什么?
  • [articleDictionary objectForKey:@"content"] 可能是nilstringWithString: 的文档说 Raises an NSInvalidArgumentException if aString is nil
  • [articleDictionary objectForKey:@"content"] 为零吗?这会导致崩溃。
  • 如果 [articleDictionary objectForKey:@"content"] 不像其他人建议的那样为零,您确定它返回的是字符串对象,而不是其他对象吗?
  • 或者你的“内容”是你在上面声明的UILabel?添加NSLog("%@", [articleDictionary objectForKey:@"content"]) 并查看它是什么(或使用调试器)。

标签: ios objective-c nsmutablestring


【解决方案1】:

试试这个:

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

如果它没有在字典中设置,它将是nil,它可以避免[NSMutableString stringWithString] 中的潜在崩溃(在nil 上调用mutableCopy 会被忽略)。

【讨论】:

  • 谢谢。这停止了​​崩溃,但应用程序挂起并引发了一个警告,如 didReceiveMemoryWarning,我怎样才能显式释放此内存。如果我说。 if(articleContent != nil) { articleContent = nil;} 这会释放内存
  • @IamVT 可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-10
  • 2012-08-18
  • 2012-02-12
  • 2014-03-18
  • 2014-11-14
相关资源
最近更新 更多