如何解决ios中的内存管理问题

时间:2016-09-27 06:33:43

标签: ios objective-c iphone

我有一个数组中的5000个对象。当我执行循环时,我的应用程序因内存问题而崩溃。

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
 NSMutableString *stringToWrite = [[NSMutableString alloc] init];
 [stringToWrite appendString:[NSString stringWithFormat:@"First Name,Last Name,Full Name,Phone Number, Email, Birthday,Job, organizationName,Note\n\n"]];
 for(int i = 0 ;i<[appDelegate.sortedFilterArray count];i++)     {
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"firstName"] objectAtIndex:i]]];
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"lastName"] objectAtIndex:i]]];
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"userName"] objectAtIndex:i]]];
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"phoneNumber"] objectAtIndex:i]]];
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"emailAddress"] objectAtIndex:i]]];
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"birthday"] objectAtIndex:i]]];
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"jobTitle"] objectAtIndex:i]]];
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"organizationName"] objectAtIndex:i]]];
      [stringToWrite appendString:[NSString stringWithFormat:@"%@\n",[[appDelegate.sortedFilterArray valueForKey:@"note"] objectAtIndex:i]]];
 }
 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
 NSString *documentDirectory=[paths objectAtIndex:0];
 NSString *strBackupFileLocation = [NSString stringWithFormat:@"%@/%@", documentDirectory,@"ContactList.csv"];
 [stringToWrite writeToFile:strBackupFileLocation atomically:YES encoding:NSUTF8StringEncoding error:nil];

1 个答案:

答案 0 :(得分:1)

您需要像这样更改for循环

for(int i = 0 ;i<[appDelegate.sortedFilterArray count];i++)
{
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray objectAtIndex:i] objectForKey:@"firstName"]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"lastName"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"userName"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"phoneNumber"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"emailAddress"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"birthday"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"jobTitle"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[appDelegate.sortedFilterArray valueForKey:@"organizationName"] objectAtIndex:i]]];
    [stringToWrite appendString:[NSString stringWithFormat:@"%@\n",[[appDelegate.sortedFilterArray valueForKey:@"note"] objectAtIndex:i]]];
}