2016-11-15 74 views
0

我有一些代碼可以創建一個字符串並將該字符串添加到可變數組中。下面是代碼:爲什麼NSMutableArray有重複的對象?

ExportBookData *abe = [[ExportBookData alloc] initWithCategory:@"ABE"]; 
    abe.builtFileList = [[NSMutableDictionary alloc] initWithCapacity: 2]; 
    abe.exportData = [NSMutableArray array]; 

     for(int i = 0; i < booksArray.count; i++) { 
     [tabString setString: @""]; // clear it... 
     [tabString appendString:[NSString stringWithFormat:@"%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t" 
           "%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\n", 

           [[booksArray objectAtIndex:i] sku], 
           [[booksArray objectAtIndex:i] title], 
           [[booksArray objectAtIndex:i] author], 
           [[booksArray objectAtIndex:i] illustrator], 
           [[booksArray objectAtIndex:i] price], 
           [(Books *)[booksArray objectAtIndex:i] quantity], 
           @"Book", // book type 
           [[booksArray objectAtIndex:i] bookDescription], 
           [[booksArray objectAtIndex:i] binding], 
           [[booksArray objectAtIndex:i] bookCondition], 
           [[booksArray objectAtIndex:i] pubName], 
           [[booksArray objectAtIndex:i]pubLocation ], 
           [[booksArray objectAtIndex:i] pubYear], 
           [[booksArray objectAtIndex:i] isbn], 
           [[booksArray objectAtIndex:i] primaryCatalog], 
           @"",@"", // seller catalogs secondary and third catalog entries (not used) 
           @"", // ABE category <----- TODO 
           [[booksArray objectAtIndex:i] keywords], 
           [[booksArray objectAtIndex:i] jacket], 
           [[booksArray objectAtIndex:i] edition], 
           [[booksArray objectAtIndex:i] printing], 
           [[booksArray objectAtIndex:i] signedBy], 
           [[booksArray objectAtIndex:i]volume], 
           [[booksArray objectAtIndex:i] bookSize], 
           @"" // image url 
           ]]; 

     NSLog(@"\n\ntabString: %@",tabString); 

     [abe.exportData addObject: tabString]; // add to array 

     NSLog(@"\n\nexportData: %@", abe.exportData); // <-------------- overwritten with last entry, so all entries are the same TODO 
    } 

booksArray是通過讀取存儲CoreData填補了的NSMutableArray; exportData也是一個NSMutableArray。 books數組中的數據是有效的,計數爲2; tabString中的數據也是有效的,包含來自booksArray的正確行。該生產線

[abe.exportData addObject: tabString]; 

擁有的最後一個記錄的booksArray重複。這是從日誌方法的輸出:

---->booksArray.count: 2 

tabString: 120 Women Who Run With The Wolves: Myths And Stories Of The Wild Woman Archetype Clarissa Pinkola Estes  2.1 1 Book  Hardcover  Ballantine Books  1992 0345377443      Like New  (null)    

exportData: (
"120\tWomen Who Run With The Wolves: Myths And Stories Of The Wild Woman Archetype\tClarissa Pinkola Estes\t\t2.1\t1\tBook\t\tHardcover\t\tBallantine Books\t\t1992\t0345377443\t\t\t\t\t\tLike New\t\t(null)\t\t\t\t\n" 
) 

tabString: 121 Colossus: The Secrets Of Bletchley Park's Code-breaking Computers B. Jack Copeland  35 1 Book  Paperback (Reprint)  Oxford University Press, USA  2010 9780199578146      No Dust Jacket  (null)    

exportData: (
"121\tColossus: The Secrets Of Bletchley Park's Code-breaking Computers\tB. Jack Copeland\t\t35\t1\tBook\t\tPaperback (Reprint)\t\tOxford University Press, USA\t\t2010\t9780199578146\t\t\t\t\t\tNo Dust Jacket\t\t(null)\t\t\t\t\n", 
"121\tColossus: The Secrets Of Bletchley Park's Code-breaking Computers\tB. Jack Copeland\t\t35\t1\tBook\t\tPaperback (Reprint)\t\tOxford University Press, USA\t\t2010\t9780199578146\t\t\t\t\t\tNo Dust Jacket\t\t(null)\t\t\t\t\n" 
) 

我都看了又看(谷歌,SO)幾個小時,現在,我是在做正確的一切,從我在其他問題的回答看,和我不看看有什麼可能造成這種情況。非常感謝幫助。 SD

回答

2

您的tabString似乎是您嘗試反覆使用的NSMutableString。這是行不通的。您最終將同一個可變字符串添加到數組中多次。只需將stringWithFormat分配到正常的本地NSString即可。

爲什麼一遍又一遍地撥打[booksArray objectAtIndex:i]?獲取對象一次。更好的是,在循環中使用快速枚舉。

for (YourBookClass *book in booksArray) { 
    NSString *tabString = [NSString stringWithFormat:@"%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t" 
          "%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\n", 
     book.sku, 
     book.title, 
     // and all of the rest of the properties 
     ]]; 

    [abe.exportData addObject:tabString]; 
} 

還請記住,如果任何值包含製表符或換行符,您的代碼將生成無效的CSV文件。這些值需要加上引號。

+0

嗨瑞克...是的,tabString的確是可變的...大鼠!再次感謝您的幫助...... SD – SpokaneDude

相關問題