2014-11-04 45 views
-1

我想將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]; 
    } 
+2

您不能重複使用' internalElement'就像你一樣。每次在'for'循環中創建一個新實例。 – rmaddy 2014-11-04 19:42:41

+0

你似乎也沒有創建'transactionDetails'數組;你正在分配它,但沒有使用它。 – 2014-11-04 19:44:53

+0

當您將一個對象放入數組中時,它只是對該對象的引用。如果您希望能夠重新使用該引用,請在將數據放入數組之前將其複製。您可以使用copy或mutableCopy方法來執行此操作。 – digidigo 2014-11-04 19:45:49

回答

3

這不是對象指針的工作方式。

考慮下面的「真實世界」的僞代碼示例:

一類的老師想從房間到另一個房間的一側移動了一堆書。她想讓所有的孩子都參與其中,所以她設計了一個計劃,讓每個孩子都拿一把書。

你的道:

1. The teacher lines up all the children. 
2. The teacher brings the first child in line over to her. Let's call him Timmy. 
3. The teacher makes sure Timmy isn't carrying a handful of books. 
4. The teacher gives Timmy a handful of books. 
5. The teacher tells Timmy to stand on her left side. 
6. Repeat steps 3-5 once for each member of the class. 

因爲老師永遠只能拿來孩子一次,它總是差鄧肯。

正確的做法:

1. The teacher lines up all the children. 
2. The teacher brings the first child in line over to her. 
3. The teacher makes sure the child isn't carrying a handful of books. 
4. The teacher gives the child a handful of books. 
5. The teacher tells the child to stand on her left side. 
6. Repeat steps 2-5 once for each member of the class. 

要解決源,交換兩行:

internalElement = [[NSMutableDictionary alloc] init]; 
for (PFObject *object in objects) { 

爲:

for (PFObject *object in objects) { 
internalElement = [[NSMutableDictionary alloc] init]; 
+0

真棒:)把我的upvote! – nburk 2014-11-04 20:34:16

+0

謝謝你的幫助! – user2985495 2014-11-04 20:56:00