NSMutableString stringWithString使应用程序崩溃

时间:2015-12-01 17:43:39

标签: ios objective-c nsmutablestring

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

var http = require('http');
var express = require('express');
var request = require('request');
var mysql = require('mysql');

var client = mysql.createConnection({
    host:'http://nodejs.somewhere.com/WebMysql',
    port : 3306,
    user : 'user',
    password : 'password',
    database  : 'database'
});
var app = express();
app.use(express.bodyParser());
app.use(app.router);

应用程序在执行第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,或者是否有任何内存泄漏,我做错了什么。 我使用nilxcode 6.3

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

这是我得到的例外。

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

1 个答案:

答案 0 :(得分:1)

请改为尝试:

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

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