【发布时间】:2016-11-15 22:00:30
【问题描述】:
我有一些代码可以创建一个字符串并将该字符串添加到一个可变数组中。代码如下:
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 是一个 NSMutableArray,它通过读取 CoreData 存储来填充; exportData 也是一个 NSMutableArray。 booksArray 中的数据有效,计数为 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"
)
我已经看了好几个小时(Google,SO),从我在其他问题的答案中看到的情况来看,我所做的一切都是正确的,我看不出是什么原因造成的。帮助将不胜感激。标清
【问题讨论】:
标签: objective-c nsmutablearray nsarray