xcode 从 NSMutableArray 中移除对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2939360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:23:58  来源:igfitidea点击:

Removing Objects From NSMutableArray

objective-ccocoaxcodecalendarnsmutablearray

提问by Garry Pettet

I have a NSMutableArray that contains all the calendars on my system (as CalCalendarobjects):

我有一个 NSMutableArray,它包含我系统上的所有日历(作为CalCalendar对象):

NSMutableArray *calendars = [[CalCalendarStore defaultCalendarStore] calendars];

NSMutableArray *calendars = [[CalCalendarStore defaultCalendarStore] calendars];

I want to remove from calendarsany CalCalendarobjects whose title does not include the string @"work".

我想从标题不包含字符串的calendars任何CalCalendar对象中删除@"work"

I've tried this:

我试过这个:

for (CalCalendar *cal in calendars) {
    // Look to see if this calendar's title contains "work". If not - remove it
    if ([[cal title] rangeOfString:@"work"].location == NSNotFound) {
        [calendars removeObject:cal];
    }
}

The console is complaining that:

控制台抱怨说:

*** Collection <NSCFArray: 0x11660ccb0> was mutated while being enumerated.

*** Collection <NSCFArray: 0x11660ccb0> was mutated while being enumerated.

And things go bad. Obviously it would seem you can't do what I want to do this way so can anyone suggest the best way to go about it?

事情变糟了。显然,您似乎无法以这种方式做我想做的事情,所以有人可以建议最好的方法吗?

Thanks,

谢谢,

回答by Georg Fritzsche

While you can not remove items in an array that you are using fast enumeration on, you have some options:

虽然您无法删除使用快速枚举的数组中的项目,但您有一些选择:

As markhunte noted, -calendarsdoesn't neccessarily return a mutable array - you'd have to use -mutableCopyto get a mutable array which you can filter:

正如 markhunte 所指出的,-calendars不一定返回可变数组 - 您必须使用它-mutableCopy来获取可以过滤的可变数组:

NSMutableArray *calendars = [[[[CalCalendarStore defaultCalendarStore] 
                                calendars] mutableCopy] autorelease];

... or e.g. -filteredArrayUsingPredicate:for a immutable filtered copy.

...或者例如-filteredArrayUsingPredicate:对于不可变的过滤副本。

NSArray *calendars = [[CalCalendarStore defaultCalendarStore] calendars];
calendars = [calendars filteredArrayUsingPredicate:myPredicate];

回答by s1mm0t

You can not change an array/list you are enumerating (in any language I know of). You will need to create a second list that you will add the calendars that you want to remove, to. Then iterate round the second list, removing the objects from the first. You can then dispose of the second list leaving just the original list with only the calendars you wish to keep hold of.

您不能更改正在枚举的数组/列表(使用我知道的任何语言)。您需要创建第二个列表,将要删除的日历添加到该列表中。然后遍历第二个列表,从第一个列表中删除对象。然后,您可以处理第二个列表,只留下原始列表和您希望保留的日历。

回答by markhunte

I noticed NSMutableArray will give the expected results from [[CalCalendarStore defaultCalendarStore] calendars]].

我注意到 NSMutableArray 将给出来自 [[CalCalendarStore defaultCalendarStore] 日历]] 的预期结果。

But the returned array from [[CalCalendarStore defaultCalendarStore] calendars]] is actually a NSArray and not an NSMutableArray. (also has a warning come up about expected structure)

但是从 [[CalCalendarStore defaultCalendarStore] calendars]] 返回的数组实际上是一个 NSArray 而不是 NSMutableArray。(还有一个关于预期结构的警告)

This is all new to me so am I missing something here? or is this the correct way of going about this task..

这对我来说是全新的,所以我在这里错过了什么吗?或者这是完成这项任务的正确方法..

NSMutableArray *workCals= [[NSMutableArray  alloc] initWithCapacity:2];
[workCals addObjectsFromArray: [[CalCalendarStore defaultCalendarStore] calendars]];


NSString *title = @"work";

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"title contains[c] %@",title ]; 
[workCals filterUsingPredicate:predicate];
NSLog(@"workCals %@",workCals );
[workCals release];

Cheers.

干杯。