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

如何解决ios中的内存管理问题,ios,objective-c,iphone,Ios,Objective C,Iphone,我有一个数组中的5000个对象。当我执行循环时,我的应用程序由于内存问题而崩溃 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; NSMutableString *stringToWrite = [[NSMutableString alloc] init]; [stringToWrite appendString:[NSString stringWithFormat

我有一个数组中的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];
AppDelegate*AppDelegate=(AppDelegate*)[[UIApplication sharedApplication]委托];
NSMutableString*stringToWrite=[[NSMutableString alloc]init];
[stringToWrite appendString:[NSString stringWithFormat:@“姓名、姓氏、全名、电话号码、电子邮件、生日、工作、组织名称、备注\n\n”];

for(inti=0;i您需要像这样更改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]]];
}

for(int i=0;我能给我们展示一下你的appDelegate.sortedFilterArray的格式吗?({birth=”“;emailAddress=”abc@gmail.com“firstName=Communication;imageData=“”;jobTitle=“”;lastName=A;note=“”;organizationName=“”;phoneIdentifier=“CDD2CE63-B50B-4E5B-B41A-D896C5BA8955”;phoneNumber=“123445677”userName=“Communication A”})这是数组中的一个对象,就像我有5000个对象进入数组一样,您的for循环中的代码是否应该反过来?即[[appDelegate.sortedFilterArray objectAtIndex:i]valueForKey:@“firstName”]而不是[[appDelegate.sortedFilterArray valueForKey:@“firstName”]objectAtIndex:i]并尝试objectForKey而不是valueForKey