Iphone 如何优化有关NSArray的代码?

Iphone 如何优化有关NSArray的代码?,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我觉得我在这里做了很多不必要的事情: - (id)init { if ((self = [super init])) { NSMutableArray *anArray = [[NSMutableArray alloc] init]; self.currentScopeArray = anArray; [anArray release]; NSString *aPath = [[NSBundle m

我觉得我在这里做了很多不必要的事情:

- (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;
}
-(id)init{
if((self=[super init]){
NSMutableArray*anArray=[[NSMutableArray alloc]init];
self.currentScopeArray=anArray;
[阵列释放];
NSString*aPath=[[NSBundle mainBundle]pathForResource:@“配置”类型:@“plist”];
NSMutableDictionary*aDictionary=[[NSMutableDictionary alloc]initWithContentsOfFile:aPath];
NSMutableArray*anotherArray=[[NSMutableArray arrayWithObject:[aDictionary objectForKey:@“Search”]]objectAtIndex:0];
self.defaultScopeArray=另一个数组;
[词典发布];
对于(int x=0;x<[self.defaultScopeArray count];x++){
BOOL enableCheckmark=[[self.defaultScopeArray objectAtIndex:x]objectForKey:@“enabledByDefault”]boolValue];
如果(启用复选标记){
如果(![self.currentScopeArray containsObject:[self.defaultScopeArray objectAtIndex:x]]){
[self.currentScopeArray addObject:[self.defaultScopeArray objectAtIndex:x]];
}
}
}
}
回归自我;
}

至少,您可以替换此块:

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

该区块:

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];
可以是:

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

通读代码并理解它的功能是一回事,但是如果您解释您试图实现的功能,您的代码可能会得到进一步优化。我也可以只做
self.defaultScopeArray=[[NSMutableArray arrayWithObject:[aDictionary objectForKey:@“Search”]]objectAtIndex:0]如果要提高性能,应该使用alloc/init和release,因为使用[NSMutableArray]之类的方法会创建自动释放的对象,这些对象在一段时间内不会释放,因此会占用内存,如果您使用alloc/init和release,它将立即被释放。我非常确信,在释放释放对象方面,实现将做任何它想做的事情。我考虑的是开发人员的生产力,在这种情况下,这肯定会超过按照您的建议进行操作可能会获得的微小收益。