通过NSArray循环

时间:2015-11-18 10:50:08

标签: objective-c nsarray fmdb

我在NSArray中遇到麻烦。

这是我的代码

 db = [[BrowserController currentBrowser] databaseOutline];
        int rowNumber = [db numberOfRows];
        int i = 0;
        if (rowNumber > 0 && i <= rowNumber)
        {
            while ([resultSet next])
            {
                NSDictionary *infos = [resultSet resultDictionary];
                for (NSString *field in fields)
                {
                    NSLog(@"field = %@", field);
                    NSString *zID = nil;
                    if ([field isEqualToString:@"ZSTUDY.ZID"])
                    {
                        zID = [infos objectForKey:[self.fieldsName objectForKey:field]];
                    }
                    NSManagedObject *curObj = [db itemAtRow:i++];
                    NSLog(@"i = %d", i);
                    NSString *browserID = [curObj valueForKey:@"StudyID"];
                    //NSString *key = [[[db sortDescriptors] objectAtIndex:0]key];
                    //                        NSLog(@"key = %@", key);
                    if ([zID isEqualToString:browserID])
                    {
                        NSLog(@"They are the same");
                        if ([field isEqualToString:@"ZSERIES.ZDATE"] || [field isEqualToString:@"ZSERIES.ZDATEADDED"] || [field isEqualToString:@"ZSERIES.ZDATEOPENED"]) {
                            NSNumber *number = [infos objectForKey:[self.fieldsName objectForKey:field]];
                            if (number && ![number isKindOfClass:[NSNull class]])
                            {
                                int result = [number intValue] + 11324*(60*60*24);
                                NSDate *date = [NSDate dateWithTimeIntervalSince1970:result];
                                [csvWriter writeField:date];
                            }
                            else
                            {
                                [csvWriter writeField:@""];
                            }
                        }
                        else
                        {
                            NSString *queryResult= [infos objectForKey:[self.fieldsName objectForKey:field]];

                            NSLog(@"result = %@", queryResult);
                            if (queryResult && ![queryResult isKindOfClass:[NSNull class]])
                            {
                                [csvWriter writeField:queryResult];
                            }
                            else
                            {
                                [csvWriter writeField:@""];
                            }
                        }
                    }
                    else
                    {
                        NSLog(@"They are different");
                        break;
                    }
                }
            }
            [csvWriter writeLine];
        }
        [csvWriter closeFile];
        [csvWriter release];
        [database close];
    }
}];

Fyi:我试图比较两个NSString *,

NSString zID* = [infos objectForKey:[self.fieldsName objectForKey:field]];

NSString *browserID = [curObj valueForKey:@"StudyID"];

这些是数据库和我的视图之间的链接。我想将数据库导出到.csv文件中,但仅当项目当前出现在视图上时。

为此,我从视图中取出了一个字段,并从数据库中获取了相同的字段。他们的字段名称不同,这就是我这样做的原因。

算法很简单,我在

中选择了我视图中的第一项
NSManagedObject *curObj = [db itemAtRow:i];

我想通过NSArray *字段

for (NSString *field in fields)

但是,NSString *字段始终具有相同的值,这是我的NSArray *字段中包含的第一个值。

这是输出:

2015-11-18 12:22:37.655 Hidden[38468:400889] field = ZSERIES.ZSTUDY
2015-11-18 12:22:37.655 Hidden[38468:400889] i = 1
2015-11-18 12:22:37.657 Hidden[38468:400889] They are different
2015-11-18 12:22:37.657 Hidden[38468:400889] field = ZSERIES.ZSTUDY
2015-11-18 12:22:37.657 Hidden[38468:400889] i = 2
2015-11-18 12:22:37.658 Hidden[38468:400889] They are different
2015-11-18 12:22:37.659 Hidden[38468:400889] field = ZSERIES.ZSTUDY
2015-11-18 12:22:37.659 Hidden[38468:400889] i = 3
2015-11-18 12:22:37.660 Hidden[38468:400889] They are different
2015-11-18 12:22:37.660 Hidden[38468:400889] field = ZSERIES.ZSTUDY
2015-11-18 12:22:37.660 Hidden[38468:400889] i = 4
2015-11-18 12:22:37.668 Hidden[38468:400889] They are different
2015-11-18 12:22:37.668 Hidden[38468:400889] field = ZSERIES.ZSTUDY
2015-11-18 12:22:37.668 Hidden[38468:400889] i = 5
2015-11-18 12:22:37.793 Hidden[38468:400889] They are different
2015-11-18 12:22:37.794 Hidden[38468:400889] field = ZSERIES.ZSTUDY
2015-11-18 12:22:37.794 Hidden[38468:400889] i = 6
2015-11-18 12:22:37.796 Hidden[38468:400889] They are different
2015-11-18 12:22:37.796 Hidden[38468:400889] field = ZSERIES.ZSTUDY
2015-11-18 12:22:37.796 Hidden[38468:400889] i = 7
2015-11-18 12:22:37.830 Hidden[38468:400889] They are different
2015-11-18 12:22:37.831 Hidden[38468:400889] field = ZSERIES.ZSTUDY
2015-11-18 12:22:37.831 Hidden[38468:400889] i = 8
2015-11-18 12:22:37.833 Hidden[38468:400889] They are different
2015-11-18 12:22:37.833 Hidden[38468:400889] field = ZSERIES.ZSTUDY
2015-11-18 12:22:37.833 Hidden[38468:400889] i = 9

我做错了什么?

如果你们要求它,我会添加更多信息。

感谢。

2 个答案:

答案 0 :(得分:0)

curObj的值未更新,因为迭代器i的值在代码中的任何位置都没有更改。它始终为0,初始化为i=0

更改此行,如下所示:

NSManagedObject *curObj = [db itemAtRow:i++];

答案 1 :(得分:0)

实际上,我的问题是break语句。

删除它,现在我的NSArray *完美循环。