简体   繁体   中英

iOS appendFomat cause app to crash

Is there anything wrong with the code below? It crash my app sometimes.

- (NSString*) getDisplayString
{
    NSMutableString* display = [[NSMutableString alloc] initWithString:@""];

for(NSUInteger i = 0; i < [entries count]; i++)
{
    Entry* e = [entries objectAtIndex:i];
    [display appendFormat:@"%d. %@ %30d\n", i+1, e.title, e.value];

}

    return display;
}

Found the answer to my problem. Issue is memory management where I wasn't increasing the retain count of the NSString title found in the Entry object. title is the value obtain from user input through a textfield so when the UIView is releasing the string which is done internally, its no longer in memory hence it makes the error appears to be random.

So to summarise whenever u are using a value from a textfield, you need to increase its retain count.

NSMutableArray* myArray = [[NSMutableArray alloc] init];
NSString* someStringToStore = uiTextField.text;
[someStringToStore retain];
[myArray addObject: someStringToStore];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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