繁体   English   中英

NSString包含一个数字,为​​什么它会使我的App崩溃?

[英]NSString contains a number, why does it crash my App?

我的应用程序在使用NSString时遇到了一些困难。 基本上,我有一个名为o1string的NSString,其中包含值“ 602”。 我想在UIAlertView中将其与其他一些文本一起输出。

votedmessage = [ NSString stringWithFormat:@"The current standings are as follows:\n\n%@: %@ votes", b1title, o1string ];
UIAlertView *votedAlert = [[UIAlertView alloc] initWithTitle:@"Thank you for voting" message:votedmessage delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

我使用了NSLog并验证了NSString中的值肯定是602,并且消息中使用的其他变量(b1title)本身可以很好地输出。 我无法弄清楚为什么在向警报消息中添加o1votes变量时应用程序崩溃了,这与在NSString中仅保存一个数字有冲突吗?

这是设置o1string的方式。 它肯定包含从XML文件获取的“ 602”。

o1string = [[options objectAtIndex:3] objectForKey: @"votes"];
o1string = [o1string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
o1string = [o1string stringByReplacingOccurrencesOfString:@"    " withString:@""];

除非对o1string的赋值与创建votedmessage的方法相同(因为您没有说,我假设不是),否则当您到达需要votedmessage的代码时,它将消失。

除非使用垃圾回收,否则您需要保留要保留在当前方法之后的对象。 有关完整的详细信息,请参见《 Objective-C内存管理指南 》。

您需要发布更多代码。 特别是不清楚您发布的两篇文章是在同一功能中还是在不同位置。

如果它们在不同的位置,则必须调用[o1string保留](以及以后的[o1string版本])。 最简单的方法是使olstring具有保留语义的属性。

stringByReplacingOccurrencesOfString返回一个临时实例,该实例将在函数存在后的某个时间自动释放。

我猜b1Title起作用的原因是它存储在您的字典中,因此是持久的。 o1string是从stringByXXX函数创建的,是临时的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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