我有一些代码可以创建一个字符串并将该字符串添加到一个可变数组中。这是代码:
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)几个小时了,我正在做的一切都正确,从我在其他问题的答案中看到的,我看不出可能导致这种情况的原因。非常感谢帮助。 SD
答案 0 :(得分: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文件。这些值需要加上引号。