Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 清除NSMutableArray_Ios_Objective C - Fatal编程技术网

Ios 清除NSMutableArray

Ios 清除NSMutableArray,ios,objective-c,Ios,Objective C,我想在internalElement中存储dateArray,然后清除dateArray,这样当for循环开始时,dateArray中就有了新的数据。目前我正试图使用[dateArray clearAllobjects]删除数据,但当我这样做时,它也会清除internalElement。有没有更好的方法来做到这一点,或者我没有得到什么。提前谢谢你的帮助 NSMutableArray *dateArray = [[NSMutableArray alloc] init]; internalE

我想在internalElement中存储dateArray,然后清除dateArray,这样当for循环开始时,dateArray中就有了新的数据。目前我正试图使用[dateArray clearAllobjects]删除数据,但当我这样做时,它也会清除internalElement。有没有更好的方法来做到这一点,或者我没有得到什么。提前谢谢你的帮助

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];
    }

这不是对象指针的工作方式

考虑以下“真实世界”伪代码示例:

一个班级的老师想把一堆书从教室的一边移到另一边。她想让所有的孩子都参与进来,所以她设计了一个计划,每个孩子都会带一把书过来

你的方式:

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) {
因为老师只接过孩子一次,所以总是可怜的提米

正确的方法:

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) {
要修复源代码,请交换以下两行:

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) {
作为:


您不能像现在这样重用
internalElement
。每次在
for
循环中创建一个新实例。您似乎也在毫无目的地创建
transactionDetails
数组;您正在分配它,但没有使用它。当您将对象放入数组时,它只是对该对象的引用。如果希望能够重用该引用,请在将数据放入阵列之前复制该数据。您可以使用copy或mutableCopy方法来执行此操作。感谢您的帮助!