2015-11-18 72 views
0

我有麻煩循環通過NSArray。雖然循環但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]; 
    } 
}]; 

供參考:我想比較兩個的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 

我做了什麼錯?

如果你們要求,我會添加更多信息。

謝謝。

+0

你永遠不會在這個代碼中增加'i'的值... 試試這個:'NSManagedObject * curObj = [db itemAtRow:i ++];' – Nishant

+0

該死的你是對的..我正在糾正它。仍然無法正常工作 – Xcrowzz

+0

很難說出所有的變量類型是什麼,但假設你有一個集合類,爲什麼不使用內置的枚舉器呢?例如' - [NSArray enumerateObjectsUsingBlock:]'。 – Avi

回答

0

其實,我的問題是break語句。

刪除它,現在我的NSArray *完美循環。

0

curObj的值未更新,因爲迭代器i的值未在代碼中的任何位置發生更改。它始終爲0,初始化爲i=0

改變這一行,如下圖所示:

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