iOS NSDictionary内存压力崩溃
我有一个非常大的NSDictionary模型,其中存储了400家公司的信息,然后在初始加载期间创建NSArray,以便在应用程序中使用以提高速度。当我有大约100家公司时,它只是运行缓慢,而不是因为内存压力而导致我的应用程序崩溃。有没有关于如何以更有效的方式构建此架构的建议iOS NSDictionary内存压力崩溃,ios,objective-c,uitableview,nsarray,nsdictionary,Ios,Objective C,Uitableview,Nsarray,Nsdictionary,我有一个非常大的NSDictionary模型,其中存储了400家公司的信息,然后在初始加载期间创建NSArray,以便在应用程序中使用以提高速度。当我有大约100家公司时,它只是运行缓慢,而不是因为内存压力而导致我的应用程序崩溃。有没有关于如何以更有效的方式构建此架构的建议 - (NSArray*)exhibitorDetail{ NSDictionary *company1 = @{@"id": @"id1", @"name": @"company xyz", @"details": @"B
- (NSArray*)exhibitorDetail{
NSDictionary *company1 = @{@"id": @"id1", @"name": @"company xyz", @"details": @"Booth: 1700", @"png": @"exhibitor_map1700.png"};
NSDictionary *company2 = @{@"id": @"id2", @"name": @"company abc", @"details": @"Booth: 1721", @"png": @"exhibitor_map1721.png"};
NSDictionary *company3 = @{@"id": @"id3", @"name": @"company mno", @"details": @"Booth: 805", @"png": @"exhibitor_map805.png"};
NSDictionary *company4 = @{@"id": @"id4", @"name": @"company efg", @"details": @"Booth: 1320, 1321", @"png": @"exhibitor_map1320.png"};
...through 400 dictionaries of companies.
NSMutableArray *allCompanies = [NSMutableArray arrayWithObjects:company1, company2, company3, company4,..]l
NSSortDescriptor *nameDescripter = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending: YES selector:@selector(caseInsensitiveCompare:)];
NSArray *descriptors = @[nameDescripter];
[allCompanies sortUsingDescriptors:descriptors];
return allCompanies;
}
然后将的字典用于UITableView
- (NSDictionary*) countPerLetter {
NSDictionary *dictionary;
NSMutableArray *objects = [[NSMutableArray alloc] init];
NSArray *letters = self.exhibitorFirstLetter;
NSArray *names = self.exhibitorName;
for (NSInteger i = 0; i < [self.exhibitorFirstLetter count]; i++)
{
[objects addObject:[[NSMutableArray alloc] init]];
}
dictionary = [[NSDictionary alloc] initWithObjects: objects forKeys:letters];
for (NSString *name in names) { //zen software, zeb, bakken
NSString *firstLetter = [name substringToIndex:1]; //z, b
for (NSString *letter in letters) { //z, b
if ([firstLetter isEqualToString:letter]) {
NSMutableArray *currentObjects = [dictionary objectForKey:letter];
[currentObjects addObject:name];
}
}
}
return dictionary;
}
你真的打了400本词典吗?不幸的是,是的。没有一种简单的加载方法,因此必须输入详细信息。我应该没有使用过NSDictionary吗?您的内存压力很大,因为您正在向dictionary添加图像。你应该直接添加图像路径而不是图像。我认为CaldiaTa在这种情况下会非常有用。@ USE3406096你应该考虑数据库,而不是数组。核心数据是一个不错的选择。
-(void) setupViewArrays {
ExhibitorData *ed = [[ExhibitorData alloc]init];
NSArray *exhibitorKeys = [ed.countPerLetter allKeys];
NSDictionary *exhibitorDictionary = [ed countPerLetter];
NSArray *exhibitorSetupArrays = @[exhibitorKeys, exhibitorDictionary];
}