Как оптимизировать этот код в отношении NSArrays? - PullRequest
0 голосов
/ 02 февраля 2012

Я чувствую, что делаю много ненужных вещей здесь:

- (id)init {
    if ((self = [super init])) {            
        NSMutableArray *anArray = [[NSMutableArray alloc] init];
        self.currentScopeArray = anArray;
        [anArray release];

        NSString *aPath = [[NSBundle mainBundle] pathForResource:@"Configuration" ofType:@"plist"];
        NSMutableDictionary *aDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:aPath];
        NSMutableArray *anotherArray = [[NSMutableArray arrayWithObject:[aDictionary objectForKey:@"Search"]] objectAtIndex:0];
        self.defaultScopeArray = anotherArray;
        [aDictionary release];

        for (int x = 0; x < [self.defaultScopeArray count]; x++) {
            BOOL enableCheckmark = [[[self.defaultScopeArray objectAtIndex:x] objectForKey:@"enabledByDefault"] boolValue];
            if (enableCheckmark) {
                if (![self.currentScopeArray containsObject:[self.defaultScopeArray objectAtIndex:x]]) {
                    [self.currentScopeArray addObject:[self.defaultScopeArray objectAtIndex:x]];
                }
            }
        }
    }
    return self;
}

1 Ответ

2 голосов
/ 02 февраля 2012

Как минимум, вы можете заменить этот блок:

NSMutableArray *anArray = [[NSMutableArray alloc] init];
self.currentScopeArray = anArray;
[anArray release];

на:

self.currentScopeArray = [NSMutableArray array];

Этот блок:

NSString *aPath = [[NSBundle mainBundle] pathForResource:@"Configuration" ofType:@"plist"];
NSMutableDictionary *aDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:aPath];
NSMutableArray *anotherArray = [[NSMutableArray arrayWithObject:[aDictionary objectForKey:@"Search"]] objectAtIndex:0];
self.defaultScopeArray = anotherArray;
[aDictionary release];

может быть:

NSString *aPath = [[NSBundle mainBundle] pathForResource:@"Configuration" ofType:@"plist"];
NSMutableDictionary *aDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:aPath];
self.defaultScopeArray = [NSMutableArray arrayWithObject:[aDictionary objectForKey:@"Search"]] objectAtIndex:0];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...