简体   繁体   English

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

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

I'm having some difficulty with NSString in my application. 我的应用程序在使用NSString时遇到了一些困难。 Basically, I have an NSString called o1string which contains the value "602". 基本上,我有一个名为o1string的NSString,其中包含值“ 602”。 I want to output this in a UIAlertView alongside some other text. 我想在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];

I have used NSLog and verified that the value inside the NSString is definitely 602, and the other variable (b1title) used in the message outputs fine on its own. 我使用了NSLog并验证了NSString中的值肯定是602,并且消息中使用的其他变量(b1title)本身可以很好地输出。 I cannot work out why the app is crashing when I add the o1votes variable to the alert message though, is it something to do with a conflict in holding just a number inside an NSString? 我无法弄清楚为什么在向警报消息中添加o1votes变量时应用程序崩溃了,这与在NSString中仅保存一个数字有冲突吗?

This is how o1string is set. 这是设置o1string的方式。 It definitely contains "602", grabbed from an XML file. 它肯定包含从XML文件获取的“ 602”。

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

Unless that assignment of o1string is in the same method where votedmessage is created (since you don't say, I'm assuming not), it will be gone by the time you get to the code where votedmessage needs it. 除非对o1string的赋值与创建votedmessage的方法相同(因为您没有说,我假设不是),否则当您到达需要votedmessage的代码时,它将消失。

Unless you're using garbage collection, you need to retain objects that you want to keep around past the current method. 除非使用垃圾回收,否则您需要保留要保留在当前方法之后的对象。 See the Objective-C memory management guide for complete details. 有关完整的详细信息,请参见《 Objective-C内存管理指南 》。

You need to post more code. 您需要发布更多代码。 In particular it's not clear whether the two pieces you posted are in the same function or different places. 特别是不清楚您发布的两篇文章是在同一功能中还是在不同位置。

If they're in different places you must call [o1string retain] (and later [o1string release]). 如果它们在不同的位置,则必须调用[o1string保留](以及以后的[o1string版本])。 The easiest way to do this would be to make olstring a property with retain semantics. 最简单的方法是使olstring具有保留语义的属性。

stringByReplacingOccurrencesOfString returns a temporary instance that will be auto-released sometime after the function exists. stringByReplacingOccurrencesOfString返回一个临时实例,该实例将在函数存在后的某个时间自动释放。

I would guess the reason b1Title works is that it's stored in your dictionary so is persistent. 我猜b1Title起作用的原因是它存储在您的字典中,因此是持久的。 o1string is created from the stringByXXX functions and is temporary. o1string是从stringByXXX函数创建的,是临时的。

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

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