为什么 NSMutableArray 有重复的对象?

Why does NSMutableArray have duplicate objects?

我有一些代码可以创建一个字符串并将该字符串添加到一个可变数组中。这是代码:

    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) 几个小时了,从我在其他问题的答案中看到的,我做的每件事都是正确的,但我看不出什么可能导致这个。帮助将不胜感激。标准差

您的 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 文件。这些值需要用引号引起来。