Curnelious
Curnelious

Reputation: 1

assign NSString

I have an NSString called animation, which is called with the following (working) code:

 animation=[rowInDataBase objectAtIndex:2] ;
 NSLog(@"animation:%@",animation);

When I try to perform the following :

previousAnimation=animation;

The previousAnimation is assigned ccsprite.
When I try to logging previousAnimation to check its value with NSLog(@"previous-animation:%@",previousAnimation);, the application crashes unless previousAnimation is NULL

What am I doing wrong in my assignment ?

Upvotes: 0

Views: 3655

Answers (3)

jbat100
jbat100

Reputation: 16827

Are you trying to copy the string? If so you should be doing:

NSString* previousAnimation = [NSString stringWithString:animation]; // autoreleased

or

NSString* previousAnimation = [animation copy]; // retain count 1, need to release

otherwise you should retain

previousAnimation = [animation retain];

and release previousAnimation when you are done.

Upvotes: 1

Simon Whitaker
Simon Whitaker

Reputation: 20566

It sounds like you're assigning a variable that has already been released, so the memory's being reused by some other random object (in the case you mentioned, a ccsprite object perhaps). But it's hard to tell for sure without seeing the code in more context.

Upvotes: 1

Joe
Joe

Reputation: 57169

animation needs to be properly retained. You should create a property with a retain attribute for animation and previousAnimation and set them like this.

self.animation = [rowInDatabase objectAtIndex:2];
...
self.previousAnimation = self.animation;

Now both values will be properly retained between calls you will no longer have crashing issues. Just remember to release both values in dealloc.

Upvotes: 3

Related Questions