iphone - Creating a long NSString causing memory issues -


My code below is causing the reason for leaving my app i.e. get the black screen and then see the program in the debugger console: Hint: A? Formula ????

Actually this is causing the problem when my order order is 2000 or more. I am using iPhone 3 GS with iOS 4.2

Q: What is my Is there a more efficient and less memory way to create long-out SRT?

  NSString * outStr = @ ""; For (int i = 0; i   

Update: Thanks to very helpful people, my modified code below is:

  NSArray * orderA = [ARAppDelegate sharedAppDelegate]. OrderArray; NSDictionary * descD = [ARAppDelegate sharedAppDelegate] .descDict; NSDictionary * priceD = [ARAppDelegate sharedAppDelegate] .priceDict; NSMutableString * outStr = [[[NSMutableString alloc] init] autorelease]; For (int i = 0; i   

// The end of this method reaches

The problem is A new string object is formed in each iteration. It consumes a lot of memory, one solution may be to use a local autoriuspool, but it is rather complex.

You should use NSMutableString , such as:

  NSMutableString * OutStr = [[[NSMutableString alloc] init] autorelease]; For (int i = 0; i   

Then you can use outStr , as if it was a NSString . As Tom comments in comments, you can convert NSMutableString to a NSString , when you are finished, using:

 < Code> NSString * result = [NSString stringWithString: outStr]; [Outrestrial release]; // & lt; - Add this line and remove autorelease // from out-stroke / int line   

to make your code easier to reuse and maintain.

Comments