2011-01-11 11 views
0

Ich habe einen Code, aber ich kann nicht sehen, wie ich es optimieren kann. Obwohl ich weiß, dass der Code schlecht geschrieben ist (von mir selbst).viewDidLoad - Code Optimierung

Ich habe eine Liste der Bahnhofsnamen. Ich möchte diese Stationsnamen zu einer Tabellenansicht hinzufügen und einen Abschnitt für den ersten Buchstaben jedes Stationsnamens haben. Diejenigen Abschnitte, die keine Stationen haben, sollten übersprungen werden.

Der erste Abschnitt sollte "-" heißen und sollte die nächstgelegene Station anzeigen.

Beispiel:

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

Also habe ich das alles funktioniert, aber ich fühle mich wie ich zu viel tue, sollte es ein einfacher Weg sein. Außerdem hängt die App für 1 Sekunde, wenn diese Ansicht auf mein iPhone 4 geladen wird. Das ist auch nicht gut. Dies ist mein Code:

- (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]; 
} 

Die dumme Sache, die ich tue, ist, dass ich Schleife durch meine Anordnung von mehr als 300 Objekten für jeden Brief, den ich habe. Aber ich kann keinen besseren Weg finden, dies zu tun.

Mit freundlichen Grüßen,
Paul Peelen

-------- -------- DAS ERGEBNIS
Dies ist, was das Ergebnis wurde dank all Ihre Antworten:

- (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

Schleife über die Stationen und füge sie dem entsprechenden Briefkorb hinzu. Sollte 26 mal schneller sein. – mvds

+0

Ich fragte dies als Antwort auf eine Antwort, aber nur für den Fall: Was ist mit der C-style Array-Syntax? Funktioniert das sogar auf NSArrays? Warum verwenden Sie nicht die Objective-C-Methoden der Klasse wie 'NSMutableArray * sectionRowArrays = [NSMutableArray initWithCapacity: 29]' und '[[sectionRowArrays objectAtIndex: index] addObject: theStation]' usw.? –

Antwort

2

Hier ist der Code mit Änderungen. Dies sind die Änderungen, die ich gemacht:

1) Verwenden Sie eine lokale Variable statt Lade sectionArray 26 mal
2) Schleife über Stationen einmal statt 29 mal

Haben Sie einen Code irgendwo verlassen? sectionSubArray ist in dieser Funktion nicht definiert, scheint jedoch eine lokale Variable zu sein. Es wird auch nie einer Eigenschaft zugewiesen.

- (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

Hallo, thnx. Ein Problem though ... error auf memset (charCounts, 0, sizeof (NSMutableArray *) * 29); "Fehler: 'charCounts' nicht deklariert (erste Verwendung in dieser Funktion)" = S –

+0

Entschuldigung, ich habe die Variablen geändert und das vergessen. Fest. – ughoavgfhw

+0

Alles klar, keine Probleme dort. Ich weiß nicht über uint8_t, aber ich weiß, dass ich eine EXC_BAD_ACCESS für diese Zeile bekomme: [sectionRowArrays [index] addObject: theStation]; –

1

Wie wäre es Iterieren über die Liste der Stationen und einfach jeden an den entsprechenden ‚Eimer‘ hinzuzufügen?

Wenn Sie NSObjects und NSArrays nicht benötigen, können Sie die Vorlage "Template Template Library" <> verwenden. Aber wenn Sie damit nicht vertraut sind, dann kann es mehr Komplexität (in Bezug auf die Lernkurve) einführen, als es wert ist.

Kurz gesagt, wie wäre es mit einem NSMutableDictionary mit NSString Schlüsseln, die erste Buchstaben und Objekte sind NSMutableArrays.

Ich habe dies nicht getan - aber es würde Ihre 30x300 Iterationen nach unten reduziert auf 300

1

Sie benötigen, um Ihre App Ausführung profilieren, wenn Sie Zeit zu verbessern Code ausgeben wollen, die Zeit in Anspruch nimmt.

Tipp 1: Verwenden Sie eine unichar hier, wird es Hunderte oder Tausende von Zuteilungen sparen:

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

Spitze 2: mehrere Ihrer Sammlungen sind bevölkert inkrementelles Wachstum verwenden. Es wird dazu beitragen, die Anzahl der Zuwächse/Reallocs nach Möglichkeit zu reduzieren. Apples Sekunde hat Sie bereits erraten, so dass ihre Optimierungen, die auf dem realen Gebrauch basieren, manchmal zu Ihren Gunsten funktionieren, aber nicht immer die ganze Zeit. Wenn Sie Ihre Sammlungen während der Population über-allokiert haben, können Sie sie reduzieren, indem Sie eine Kopie erstellen und dann das temporäre Objekt entfernen.

Tipp 3: Vermeiden Sie nach Möglichkeit automatisch freigegebene Objekte. (Ernsthaft)

natürlich wollen Sie auch Ihre Suche/Sortierung/Bestückung von Algos optimieren.

aber ernsthaft, Sie müssen Profil, wenn Sie wissen möchten, wo Ihre Zeit verbracht wird.

viel Glück!

+1

+1 Große Tipps. Ich wollte Tipp 3 wiederholen: Benutze nur Autorelease, wo du eigentlich keine andere Wahl hast. Wenn Sie es selbst löschen, sobald Sie es nicht mehr benötigen, wird der Gesamtspeicherverbrauch und (in geringerem Maße) die CPU-Last reduziert. –

1

Hinzufügen einfach auf andere Antworten, wie dies eine viel kleinere Sache ist, und ist nicht annähernd so wichtig, sondern etwas, das man zumindest wissen sollte.

Dieses:

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:@"Ö"]; 

kann mit dieser Fassung:

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]; 

Beseitigt die Notwendigkeit für ein veränderliches Array (Verringerung der Overhead) und, wenn der Compiler jene 30 addObject nicht optimieren würde Anrufe, beschleunigt die Dinge ein wenig. Wenn Sie den Inhalt von NSArray (oder NSString oder NSDictionary usw.) nach der Initialisierung nicht ändern müssen, verwenden Sie die Mutable-Version nicht, wenn Sie ihnen helfen können.

+0

Sie müssen am Ende des Aufrufs NULL hinzufügen, damit die Methode weiß, wo sie anhalten soll. – ughoavgfhw

+0

@ughoavgfhw Ups, genau richtig! Das ist, was ich für das Posten beim Kochen des Abendessens bekomme. Ich habe den Code repariert. –

+0

Das habe ich auch gesehen. Ich habe es im Code geändert. –