我正在尝试编写一个例程,该例程将从制表符分隔的文件中将数百个客户记录导入到NSArrayController中。

这是我的初始代码:

NSString *stringFromFile = [NSString stringWithContentsOfFile:[fileURL path]
                                encoding:NSUTF8StringEncoding error:&error];
NSArray *paragraphsArray = [stringFromFile componentsSeparatedByString:@"\r"];
NSMutableDictionary *destDict = [[NSMutableDictionary alloc] init];

int i;
for (i = 0; i <= [paragraphsArray count]-1; i++) {
    NSString *currentParagraph = [paragraphsArray objectAtIndex:i];
    if ([currentParagraph length ] > 4) {
        NSArray *elementsArray = [currentParagraph componentsSeparatedByString:@"\t"];
        if ([elementsArray count] > 0) {
            currentSerial++;
            [destDict setObject:[NSString stringWithFormat:@"%ld",currentSerial]
                         forKey:@"idSerial"];
            [destDict setObject:[elementsArray objectAtIndex:1]
                         forKey:@"destRazaoSocial"];
            [destDict setObject:[elementsArray objectAtIndex:2]
                         forKey:@"destLogradouro"];
            [destDict setObject:[elementsArray objectAtIndex:3]
                         forKey:@"destLogradouroNro"];
            /// repeat this for every field...
            //Add the record:
            [destArrayController addObject:destDict];
        }
    }
}


此代码导致记录被错误地插入,如果我在一个示例中有4条记录,则插入的4条记录的内容将与最后一条记录相同。

但是,如果我将代码更改为此,分配destDict并在每个循环中将其丢弃,则它可以工作:

NSString *stringFromFile = [NSString stringWithContentsOfFile:[fileURL path]
                                   encoding:NSUTF8StringEncoding error:&error];
NSArray *paragraphsArray = [stringFromFile componentsSeparatedByString:@"\r"];
int i;
for (i = 0; i <= [paragraphsArray count]-1; i++) {
    NSString *currentParagraph = [paragraphsArray objectAtIndex:i];
    if ([currentParagraph length ] > 4) {
        NSArray *elementsArray = [currentParagraph componentsSeparatedByString:@"\t"];
        if ([elementsArray count] > 0) {
            NSMutableDictionary *destDict = [[NSMutableDictionary alloc] init];
            currentSerial++;
            [destDict setObject:[NSString stringWithFormat:@"%ld",currentSerial]
                         forKey:@"idSerial"];
            [destDict setObject:[elementsArray objectAtIndex:1]
                         forKey:@"destRazaoSocial"];
            [destDict setObject:[elementsArray objectAtIndex:2]
                         forKey:@"destLogradouro"];
            [destDict setObject:[elementsArray objectAtIndex:3]
                         forKey:@"destLogradouroNro"];
            /// repeat this for every field...
            //Add the record:
            [destArrayController addObject:destDict];
            [destDict release];
        }
    }
}


这可行,但对我来说看起来并不优雅。
您愿意告诉我正确的方法吗?

最佳答案

使用您发布的第二种方法没有错。另一种方法(可能会更好,也可能不会更好)是第一种方法,但是在数组中添加[destDict copy]而不是destDict。

关于for循环的另一点-如果不需要使用代码中没有的索引,则应使用快速枚举。它更快,更容易编写。所以这:

int i;
for (i = 0; i <= [paragraphsArray count]-1; i++) {
    NSString *currentParagraph = [paragraphsArray objectAtIndex:i];


可以这样进行(称为for-in循环):

for (NSString *currentParagraph in paragraphArray){
    ....
    ....
}

10-08 05:28