2011-01-11 134 views
0

我有一些代码,但我看不到如何优化它。 Allthough我知道代码写得很糟(我自己)。viewDidLoad - 代码优化

我所拥有的是火车站名称列表。我想要做的就是将这些电台名称添加到桌面视图中,并为每个电台名称的第一个字母设置一个部分。应该跳过那些没有任何电台的部分。

第一部分应该被称为“ - ”,并应显示最近的车站。

例子:

--A-- 
Astation C 
Alaska C 
Alabama C 
--C-- 
Cstation 
Cathedral station 
Central station 

所以我得到了这一切工作,但我觉得我做的不怎么样,应该有一个更简单的方法。此外,在我的iPhone 4上加载此视图时,该应用程序会挂起1秒钟。这也不算什么。 这是我的代码:

- (void)viewDidLoad{ 
    self.filteredListContent = [NSMutableArray array]; 

    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch 
                        target:self 
                        action:@selector(searchBar:)]; 
    self.navigationItem.rightBarButtonItem = rightBarButton; 

    [rightBarButton release]; 

    UISearchBar *mySearchBar = [[UISearchBar alloc] init]; 
    mySearchBar.delegate = self; 
    [mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone]; 
    [mySearchBar sizeToFit]; 
    theTable.tableHeaderView = mySearchBar; 

    if (UIInterfaceOrientationLandscapeRight == [[UIDevice currentDevice] orientation] || 
     UIInterfaceOrientationLandscapeLeft == [[UIDevice currentDevice] orientation]) 
    { 
     theTable.tableHeaderView.frame = CGRectMake(0.f, 0.f, 480.f, 44.f); 
    } 
    else 
    { 
     theTable.tableHeaderView.frame = CGRectMake(0.f, 0.f, 320.f, 44.f); 
    } 

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self]; 
    [self setSearchDisplayController:searchDisplayController]; 
    [searchDisplayController setDelegate:self]; 
    [searchDisplayController setSearchResultsDataSource:self]; 

    [mySearchBar release]; 

    /* Set the data */ 
    NSArray *stationenPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *stationenDocumentsDirectory = [stationenPath objectAtIndex:0]; 
    NSString *path = [stationenDocumentsDirectory stringByAppendingPathComponent:@"stations.plist"]; 

    NSDictionary* stationenDictionary = [[[NSDictionary alloc] initWithContentsOfFile:path] autorelease]; 
    xmlStationenList* stationsXml = [[xmlStationenList alloc] initWithDictionary:stationenDictionary]; 

    NSSortDescriptor *stationSorter = [[NSSortDescriptor alloc] initWithKey:@"_station" ascending:YES]; 
    NSMutableArray *xmlResult = [stationsXml getTimeResult]; 

    NSArray *objects = [[[NSArray alloc] initWithObjects:stationSorter,nil] autorelease]; 
    [xmlResult sortUsingDescriptors:objects]; 

    [stationSorter release]; 

    /* prefName is decided by the page opening chooseStationViewController. Using this class init function */ 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    self.prefValue = [prefs valueForKey:prefName]; 

    self.listContent = [NSMutableArray array]; 

    for (stationenListSet *theList in xmlResult) 
    { 
     [self.listContent addObject:[theList get_station]]; 
    } 
    /* END set the data */ 

    /* Set sections */ 
    self.sectionArray = [NSMutableArray array]; 
    [self.sectionArray addObject:@"-"]; 
    [self.sectionArray addObject:@"A"]; 
    [self.sectionArray addObject:@"B"]; 
    [self.sectionArray addObject:@"C"]; 
    [self.sectionArray addObject:@"D"]; 
    [self.sectionArray addObject:@"E"]; 
    [self.sectionArray addObject:@"F"]; 
    [self.sectionArray addObject:@"G"]; 
    [self.sectionArray addObject:@"H"]; 
    [self.sectionArray addObject:@"I"]; 
    [self.sectionArray addObject:@"J"]; 
    [self.sectionArray addObject:@"K"]; 
    [self.sectionArray addObject:@"L"]; 
    [self.sectionArray addObject:@"M"]; 
    [self.sectionArray addObject:@"N"]; 
    [self.sectionArray addObject:@"O"]; 
    [self.sectionArray addObject:@"P"]; 
    [self.sectionArray addObject:@"Q"]; 
    [self.sectionArray addObject:@"R"]; 
    [self.sectionArray addObject:@"S"]; 
    [self.sectionArray addObject:@"T"]; 
    [self.sectionArray addObject:@"U"]; 
    [self.sectionArray addObject:@"V"]; 
    [self.sectionArray addObject:@"W"]; 
    [self.sectionArray addObject:@"X"]; 
    [self.sectionArray addObject:@"Y"]; 
    [self.sectionArray addObject:@"Z"]; 
    [self.sectionArray addObject:@"Å"]; 
    [self.sectionArray addObject:@"Ä"]; 
    [self.sectionArray addObject:@"Ö"]; 

    [sectionSubArray release]; 
    sectionSubArray = [[NSMutableArray alloc] init]; 
    NSMutableArray *sectionTempArray = [[NSMutableArray alloc] init]; 

    [sectionTempArray addObject:@"-"]; 
    [sectionSubArray addObject:[[NSArray alloc] init]]; 

    for(NSString *sectionChar in self.sectionArray) 
    { 
     sectionChar = [sectionChar uppercaseString]; 
     int i = 0; 

     NSMutableArray *sectionRowArray = [[NSMutableArray alloc] init]; 
     for (NSString* theStation in listContent) 
     { 
      NSString *firstChar = [[NSString alloc] initWithFormat:@"%C", toupper([theStation characterAtIndex:0])]; 

      if([firstChar isEqualToString:sectionChar]) 
      { 
       [sectionRowArray addObject:theStation]; 

       i++; 
      } 

      [firstChar release]; 
     } 

     if(i > 0) 
     { 
      [sectionSubArray addObject:sectionRowArray]; 
      [sectionTempArray addObject:sectionChar]; 
     } 

     [sectionRowArray release]; 
    } 

    self.sectionArray = sectionTempArray; 
    [sectionTempArray release]; 

    [theTable reloadData]; 
    theTable.scrollEnabled = YES; 
    [stationsXml release]; 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate=self; 
    locationManager.desiredAccuracy=kCLLocationAccuracyBest; 

    [locationManager startUpdatingLocation]; 
} 

我做的愚蠢的事情是,我环路经过我的300对象的每一封信我的阵列。但我找不到一个更好的方法来做到这一点。

最好的问候,
保罗Peelen

--------结果--------
这是个什么结果变成了,感谢所有的答案:

- (void)viewDidLoad{ 
    self.filteredListContent = [NSMutableArray array]; 

    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch 
                        target:self 
                        action:@selector(searchBar:)]; 
    self.navigationItem.rightBarButtonItem = rightBarButton; 

    [rightBarButton release]; 

    UISearchBar *mySearchBar = [[UISearchBar alloc] init]; 
    mySearchBar.delegate = self; 
    [mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone]; 
    [mySearchBar sizeToFit]; 
    theTable.tableHeaderView = mySearchBar; 

    if (UIInterfaceOrientationLandscapeRight == [[UIDevice currentDevice] orientation] || 
     UIInterfaceOrientationLandscapeLeft == [[UIDevice currentDevice] orientation]) 
    { 
     theTable.tableHeaderView.frame = CGRectMake(0.f, 0.f, 480.f, 44.f); 
    } 
    else 
    { 
     theTable.tableHeaderView.frame = CGRectMake(0.f, 0.f, 320.f, 44.f); 
    } 

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self]; 
    [self setSearchDisplayController:searchDisplayController]; 
    [searchDisplayController setDelegate:self]; 
    [searchDisplayController setSearchResultsDataSource:self]; 

    [mySearchBar release]; 

    /* Set the data */ 
    NSArray *stationenPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *stationenDocumentsDirectory = [stationenPath objectAtIndex:0]; 
    NSString *path = [stationenDocumentsDirectory stringByAppendingPathComponent:@"stations.plist"]; 

    NSDictionary* stationenDictionary = [[[NSDictionary alloc] initWithContentsOfFile:path] autorelease]; 
    xmlStationenList* stationsXml = [[xmlStationenList alloc] initWithDictionary:stationenDictionary]; 

    NSSortDescriptor *stationSorter = [[NSSortDescriptor alloc] initWithKey:@"_station" ascending:YES]; 
    NSMutableArray *xmlResult = [stationsXml getTimeResult]; 

    NSArray *objects = [[[NSArray alloc] initWithObjects:stationSorter,nil] autorelease]; 
    [xmlResult sortUsingDescriptors:objects]; 

    [stationSorter release]; 

    /* prefName is decided by the page opening chooseStationViewController. Using this class init function */ 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    self.prefValue = [prefs valueForKey:prefName]; 

    self.listContent = [NSMutableArray array]; 

    for (stationenListSet *theList in xmlResult) 
    { 
     [self.listContent addObject:[theList get_station]]; 
    } 
    /* END set the data */ 

    /* Set sections */ 
    self.sectionArray = [[NSArray alloc] initWithObjects:@"-", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", @"Å", @"Ä", @"Ö", nil]; 

    NSMutableDictionary *sectionDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
               [[NSMutableArray alloc] init], @"-", 
               [[NSMutableArray alloc] init], @"A", 
               [[NSMutableArray alloc] init], @"B", 
               [[NSMutableArray alloc] init], @"C", 
               [[NSMutableArray alloc] init], @"D", 
               [[NSMutableArray alloc] init], @"E", 
               [[NSMutableArray alloc] init], @"F", 
               [[NSMutableArray alloc] init], @"G", 
               [[NSMutableArray alloc] init], @"H", 
               [[NSMutableArray alloc] init], @"I", 
               [[NSMutableArray alloc] init], @"J", 
               [[NSMutableArray alloc] init], @"K", 
               [[NSMutableArray alloc] init], @"L", 
               [[NSMutableArray alloc] init], @"M", 
               [[NSMutableArray alloc] init], @"N", 
               [[NSMutableArray alloc] init], @"O", 
               [[NSMutableArray alloc] init], @"P", 
               [[NSMutableArray alloc] init], @"Q", 
               [[NSMutableArray alloc] init], @"R", 
               [[NSMutableArray alloc] init], @"S", 
               [[NSMutableArray alloc] init], @"T", 
               [[NSMutableArray alloc] init], @"U", 
               [[NSMutableArray alloc] init], @"V", 
               [[NSMutableArray alloc] init], @"W", 
               [[NSMutableArray alloc] init], @"X", 
               [[NSMutableArray alloc] init], @"Y", 
               [[NSMutableArray alloc] init], @"Z", 
               [[NSMutableArray alloc] init], @"Å", 
               [[NSMutableArray alloc] init], @"Ä", 
               [[NSMutableArray alloc] init], @"Ö", nil]; 

    [sectionSubArray release]; 
    sectionSubArray = [[NSMutableArray alloc] init]; 

    for(NSString *theStation in listContent) { 
     NSString *firstChar = [[[NSString alloc] initWithFormat:@"%C", toupper([theStation characterAtIndex:0])] uppercaseString]; 

     [[sectionDictionary objectForKey:firstChar] addObject:theStation]; 
    } 

    NSArray *keys = [sectionDictionary allKeys]; 
    keys = [keys sortedArrayUsingSelector:@selector(localizedCompare:)]; 

    int indexPath = 0; 

    NSMutableArray *sectionTempArray = [[NSMutableArray alloc] init]; 

    [sectionTempArray addObject:@"-"]; 
    [sectionSubArray addObject:[[NSArray alloc] init]]; 

    for(NSString *key in keys) 
    { 
     if ([[sectionDictionary objectForKey:key] count] > 0) 
     { 
      NSArray *subArray = [[sectionDictionary objectForKey:key] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

      [sectionTempArray addObject:key]; 
      [sectionSubArray addObject:subArray]; 
     } 

     indexPath++; 
    } 

    self.sectionArray = sectionTempArray; 
    [sectionTempArray release]; 

    [theTable reloadData]; 
    theTable.scrollEnabled = YES; 
    [stationsXml release]; 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate=self; 
    locationManager.desiredAccuracy=kCLLocationAccuracyBest; 

    [locationManager startUpdatingLocation]; 
} 
+4

在站上循环并将它们添加到适当的字母桶中。应该快26倍。 – mvds 2011-01-11 00:30:34

+0

我在回答这个问题时提出了这个问题,但以防万一:C语言风格的数组语法是什么?这甚至在NSArrays上工作吗?为什么不使用类的标准Objective-C方法,比如`NSMutableArray * sectionRowArrays = [NSMutableArray initWithCapacity:29]`,和[[SectionRowArrays objectAtIndex:index] addObject:theStation]`等等? – 2011-01-11 10:45:13

回答

2

这是带有fiew变化的代码。这是我所做的更改:

1)你有没有留下一些代码某处出使用局部变量,而不是装载sectionArray 26倍
2)遍历站一次,而不是29倍

? sectionSubArray未在此函数中定义,但似乎是局部变量。它也从未被分配到一个财产。

- (void)viewDidLoad{ 
    self.filteredListContent = [NSMutableArray array]; 

    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch 
                       target:self 
                       action:@selector(searchBar:)]; 
    self.navigationItem.rightBarButtonItem = rightBarButton; 

    [rightBarButton release]; 

    UISearchBar *mySearchBar = [[UISearchBar alloc] init]; 
    mySearchBar.delegate = self; 
    [mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone]; 
    [mySearchBar sizeToFit]; 
    theTable.tableHeaderView = mySearchBar; 

    if (UIInterfaceOrientationLandscapeRight == [[UIDevice currentDevice] orientation] || 
    UIInterfaceOrientationLandscapeLeft == [[UIDevice currentDevice] orientation]) 
    { 
     theTable.tableHeaderView.frame = CGRectMake(0.f, 0.f, 480.f, 44.f); 
    } 
    else 
    { 
     theTable.tableHeaderView.frame = CGRectMake(0.f, 0.f, 320.f, 44.f); 
    } 

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self]; 
    [self setSearchDisplayController:searchDisplayController]; 
    [searchDisplayController setDelegate:self]; 
    [searchDisplayController setSearchResultsDataSource:self]; 

    [mySearchBar release]; 

    /* Set the data */ 
    NSArray *stationenPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *stationenDocumentsDirectory = [stationenPath objectAtIndex:0]; 
    NSString *path = [stationenDocumentsDirectory stringByAppendingPathComponent:@"stations.plist"]; 

    NSDictionary* stationenDictionary = [[[NSDictionary alloc] initWithContentsOfFile:path] autorelease]; 
    xmlStationenList* stationsXml = [[xmlStationenList alloc] initWithDictionary:stationenDictionary]; 

    NSSortDescriptor *stationSorter = [[NSSortDescriptor alloc] initWithKey:@"_station" ascending:YES]; 
    NSMutableArray *xmlResult = [stationsXml getTimeResult]; 

    NSArray *objects = [[[NSArray alloc] initWithObjects:stationSorter,nil] autorelease]; 
    [xmlResult sortUsingDescriptors:objects]; 

    [stationSorter release]; 

    /* prefName is decided by the page opening chooseStationViewController. Using this class init function */ 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    self.prefValue = [prefs valueForKey:prefName]; 

    self.listContent = [NSMutableArray array]; 

    for (stationenListSet *theList in xmlResult) 
    { 
     [self.listContent addObject:[theList get_station]]; 
    } 
    /* END set the data */ 

    /* Set sections */ 
    NSMutableArray *sectionTempArray = [NSMutableArray array]; 
    self.sectionArray = sectionTempArray; 
    [sectionTempArray addObject:@"-"]; 
    [sectionTempArray addObject:@"A"]; 
    [sectionTempArray addObject:@"B"]; 
    [sectionTempArray addObject:@"C"]; 
    [sectionTempArray addObject:@"D"]; 
    [sectionTempArray addObject:@"E"]; 
    [sectionTempArray addObject:@"F"]; 
    [sectionTempArray addObject:@"G"]; 
    [sectionTempArray addObject:@"H"]; 
    [sectionTempArray addObject:@"I"]; 
    [sectionTempArray addObject:@"J"]; 
    [sectionTempArray addObject:@"K"]; 
    [sectionTempArray addObject:@"L"]; 
    [sectionTempArray addObject:@"M"]; 
    [sectionTempArray addObject:@"N"]; 
    [sectionTempArray addObject:@"O"]; 
    [sectionTempArray addObject:@"P"]; 
    [sectionTempArray addObject:@"Q"]; 
    [sectionTempArray addObject:@"R"]; 
    [sectionTempArray addObject:@"S"]; 
    [sectionTempArray addObject:@"T"]; 
    [sectionTempArray addObject:@"U"]; 
    [sectionTempArray addObject:@"V"]; 
    [sectionTempArray addObject:@"W"]; 
    [sectionTempArray addObject:@"X"]; 
    [sectionTempArray addObject:@"Y"]; 
    [sectionTempArray addObject:@"Z"]; 
    [sectionTempArray addObject:@"Å"]; 
    [sectionTempArray addObject:@"Ä"]; 
    [sectionTempArray addObject:@"Ö"]; 

    [sectionSubArray release]; 
    sectionSubArray = [[NSMutableArray alloc] init]; 
    sectionTempArray = [[NSMutableArray alloc] init]; 

    [sectionTempArray addObject:@"-"]; 
    [sectionSubArray addObject:[[NSArray alloc] init]]; 

    NSMutableArray *sectionRowArrays[29]; 
    memset(sectionRowArrays,0,sizeof(NSMutableArray*)*29); 
    for(NSString *theStation in listContent) { 
     char currChar = toupper([theStation characterAtIndex:0]); 
     uint8_t index; 
     if(currChar == 'Å') index = 26; 
      else if(currChar == 'Ä') index = 27; 
      else if(currChar == 'Ö') index = 28; 
      else index = currChar - 'A'; 
     if(!sectionRowArrays[index]) sectionRowArrays[index] = [[NSMutableArray alloc] init]; 
     [sectionRowArrays[index] addObject:theStation]; 
    } 
    for(uint8_t index = 0; index < 29; ++index) { 
     if(sectionRowArrays[index]) { 
      [sectionSubArray addObject:sectionRowArrays[index]]; 
      NSString *currChar; 
      if(index == 26) currChar = @"Å"; 
       else if(index == 27) currChar = @"Ä"; 
       else if(index == 28) currChar = @"Ö"; 
       else currChar = [NSString stringWithFormat:@"%c",index+'A']; 
      [sectionTempArray addObject:currChar]; 
      [sectionRowArrays[index] release]; 
     } 
    } 

    self.sectionArray = sectionTempArray; 
    [sectionTempArray release]; 

    [theTable reloadData]; 
    theTable.scrollEnabled = YES; 
    [stationsXml release]; 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate=self; 
    locationManager.desiredAccuracy=kCLLocationAccuracyBest; 

    [locationManager startUpdatingLocation]; 
} 
+0

嗨,thnx。一个问题虽然...错误在memset(charCounts,0,sizeof(NSMutableArray *)* 29); “错误:'charCounts'未声明(首次在此函数中使用)”= S – 2011-01-11 08:44:53

1

如何遍历站列表并简单地将每个添加到适当的“桶”?

如果你不需要NSObject和NSArrays,那么你可以考虑使用标准模板库'map <>模板。但是,如果你不熟悉这一点,那么它可能会引入更多的复杂性(在学习曲线方面),而不是它的价值。

简而言之,如何使用NSString键作为第一个字母和对象是NSMutableArrays的NSMutableDictionary。

我没有这样做 - 但它会降低你的30x300迭代下降到300

1

你需要,如果你想花时间来提高代码,需要一定的时间来分析您的应用程序的执行。

提示1:使用单字符在这里,这将节省分配的数百或数千个:

NSString *firstChar = [[NSString alloc] initWithFormat:@"%C", toupper([theStation characterAtIndex:0])]; 

提示2:你的一些藏品在使用增量增长填充。这将有助于尽可能减少增长/重新分配的数量。苹果第二次已经猜到了你,所以他们的优化基于现实世界的使用,有时候会对你有利,但不是所有的时候都可能。如果您在人口中过度分配收藏,则可以通过创建副本然后处置临时图来将其平铺。

提示3:尽可能避免自动释放对象。 (严重)

当然,你也想要优化你的搜索/排序/填充算法。

但严重的是,如果您想了解您的时间花费在哪里,则必须进行配置。

祝你好运!

+1

+1大提示。我想重申提示3:只有在实际上没有其他选择的情况下才使用autorelease。在不再需要它的时候自行释放它将减少总内存使用量和(在较小程度上)CPU负载。 – 2011-01-11 02:55:40

1

到其他的答案就增加,因为这是一个非常小的事情,是不是几乎一样重要的事,但你至少应该知道。

此:

NSMutableArray *sectionTempArray = [NSMutableArray array]; 
self.sectionArray = sectionTempArray; 
[sectionTempArray addObject:@"-"]; 
[sectionTempArray addObject:@"A"]; 
[sectionTempArray addObject:@"B"]; 
[sectionTempArray addObject:@"C"]; 
[sectionTempArray addObject:@"D"]; 
[sectionTempArray addObject:@"E"]; 
[sectionTempArray addObject:@"F"]; 
[sectionTempArray addObject:@"G"]; 
[sectionTempArray addObject:@"H"]; 
[sectionTempArray addObject:@"I"]; 
[sectionTempArray addObject:@"J"]; 
[sectionTempArray addObject:@"K"]; 
[sectionTempArray addObject:@"L"]; 
[sectionTempArray addObject:@"M"]; 
[sectionTempArray addObject:@"N"]; 
[sectionTempArray addObject:@"O"]; 
[sectionTempArray addObject:@"P"]; 
[sectionTempArray addObject:@"Q"]; 
[sectionTempArray addObject:@"R"]; 
[sectionTempArray addObject:@"S"]; 
[sectionTempArray addObject:@"T"]; 
[sectionTempArray addObject:@"U"]; 
[sectionTempArray addObject:@"V"]; 
[sectionTempArray addObject:@"W"]; 
[sectionTempArray addObject:@"X"]; 
[sectionTempArray addObject:@"Y"]; 
[sectionTempArray addObject:@"Z"]; 
[sectionTempArray addObject:@"Å"]; 
[sectionTempArray addObject:@"Ä"]; 
[sectionTempArray addObject:@"Ö"]; 

可与此替换:

NSArray sectionTempArray = [[NSArray alloc] initWithObjects: @"-", @"A", @"B", 
    @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", 
    @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", 
    @"Å", @"Ä", @"Ö", nil]; 

消除用于可变数组的需要(减少开销)和,如果编译器不会优化掉那些30 ADDOBJECT电话,加快了一点。如果在初始化后不需要更改NSArray(或NSString或NSDictionary等)的内容,请不要使用Mutable版本(如果可以的话)。