views:

455

answers:

2

I'm having some difficulty with NSString in my application. Basically, I have an NSString called o1string which contains the value "602". I want to output this in a UIAlertView alongside some other text.

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. 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?

This is how o1string is set. It definitely contains "602", grabbed from an XML file.

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

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.

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.

Chuck
It isn't set in the same method, but it is set in the same place as b1title, which works fine. Both are initially declared in the header file.
FishFingers
If b1title is set the same way and it works, then you're just getting lucky, because your code is wrong.
Chuck
+1 I think the variable is being released previous to the alertview being created.
Jab
+1 you need the retain the string returned by stringByReplacing...Best to use properties, and use dot notation so you don't leak memory by forgetting to release old o1string before assigning it a new value
freespace
But it's important to understand memory management even if you use properties.
Chuck
Thanks Chuck.I added o1string = [o1string retain]; to the end of those three lines of code and it now works.
FishFingers
@fish: b1title most likely works because you are not mutating it. When you mutate the object you got from your array (where it was retained) with the stringByReplacing... you end up with an autoreleased string which is going away before you use it.
Jason Coco
@fish: also, don't forget to release o1string after you use it now to set the votedMessage...
Jason Coco
A: 

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]). The easiest way to do this would be to make olstring a property with retain semantics.

stringByReplacingOccurrencesOfString returns a temporary instance that will be auto-released sometime after the function exists.

I would guess the reason b1Title works is that it's stored in your dictionary so is persistent. o1string is created from the stringByXXX functions and is temporary.

Andrew Grant