0

I am really confused any help will be highly appreciated

So what is the difference below

int myInteger = 1;
myString = [NSString stringWithFormat:@"anotesound%i",myInteger];

and this

myString = [NSString stringWithString:@"anotesound1"];

I thought both should be same but open al do not accept those are equal, it works for the stringwithstring but not works for stringwithformat

3
  • how does it not work? compiler error or crash or what does your result string look like? Have you tried myString = [NSString stringWithFormat: @"anotesound%d",myInteger]; ? Commented Nov 25, 2011 at 21:25
  • Those will output the same thing "anotesound1". Not sure what your question is. Commented Nov 25, 2011 at 21:26
  • NSLog(@"%@",myString); gives the same output that is what i am hitting my head to the wall for 2 hours. Also i try with %d not working... I have a class Sound manager and at that class on this line NSNumber *numVal = [soundLibrary objectForKey:aSoundKey]; EXC_BAD_ACCESS error occurs. Thank you for your answer Commented Nov 25, 2011 at 21:31

2 Answers 2

1

stringWithFormat: always returns a newly created and autoreleased object, which will eventually be released and become a zombie. stringWithString: in this case returns the literal (constant) object itself, which will never been released.

I'd recommend you to learn the memory management of iOS, or use ARC. http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/

Sign up to request clarification or add additional context in comments.

Comments

0

The results are the same, the error is elsewhere.

The difference is in wether or not myInteger is a constant or fill change on different instantiations. If it is always the same just use:

myString = @"anotesound1";

There is no need for the stringWithString method when the string is a constant.

4 Comments

Thank you anyway still not figure it out.
string is not constant, it is changed by a switch 1 or 2
Run the Analyzer and turn on NSZombies. The difference in code it just changing timings enough to cause the bug to appear.
Thank you so much it found a zombie. An Objective-C message was sent to a deallocated object (zombie) at address: 0xf37d820. Now what should i do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.