我想將dateArray存儲在internalElement中,然後清除dateArray,因此當for循環重新開始時,我在dateArray中有新數據。目前我正在嘗試使用[dateArray clearAllobjects]來刪除數據,但是當我這樣做時它也清除了internalElement。有沒有更好的方法來做到這一點,或我沒有得到什麼。感謝您的幫助提前。清除NSMutableArray
NSMutableArray *dateArray = [[NSMutableArray alloc] init];
internalElement = [[NSMutableDictionary alloc] init];
for (PFObject *object in objects) {
newDate = object[@"date"];
if([oldDate isEqual:@""]) {
NSMutableArray *transactionDetails = [[NSMutableArray alloc] init];
[transactionDetails addObject:object[@"amount"]];
[transactionDetails addObject:object[@"memo"]];
[transactionDetails addObject:object[@"category"][@"name"]];
[dateArray addObject:transactionDetails];
oldDate = object[@"date"];
} else if([newDate isEqual:oldDate]) {
NSMutableArray *transactionDetails = [[NSMutableArray alloc] init];
[transactionDetails addObject:object[@"amount"]];
[transactionDetails addObject:object[@"memo"]];
[transactionDetails addObject:object[@"category"][@"name"]];
oldDate = object[@"date"];
} else {
[internalElement setObject:dateArray forKey:oldDate];
NSLog(@"Date Array%@", internalElement);
[dateArray removeAllObjects];
NSLog(@"Date Array%@", internalElement);
NSMutableArray *transactionDetails = [[NSMutableArray alloc] init];
[transactionDetails addObject:object[@"amount"]];
[transactionDetails addObject:object[@"memo"]];
[transactionDetails addObject:object[@"category"][@"name"]];
[dateArray addObject:transactionDetails];
//[transactionDetails removeAllObjects];
oldDate = object[@"date"];
}
[internalElement setObject:object[@"date"] forKey:@"date"];
[internalElement setObject:object[@"amount"] forKey:@"amount"];
[transactionArray addObject:internalElement];
}
您不能重複使用' internalElement'就像你一樣。每次在'for'循環中創建一個新實例。 – rmaddy 2014-11-04 19:42:41
你似乎也沒有創建'transactionDetails'數組;你正在分配它,但沒有使用它。 – 2014-11-04 19:44:53
當您將一個對象放入數組中時,它只是對該對象的引用。如果您希望能夠重新使用該引用,請在將數據放入數組之前將其複製。您可以使用copy或mutableCopy方法來執行此操作。 – digidigo 2014-11-04 19:45:49