ios中的removeObject
我的程序有一个名为“matchedCards”的NSMutable数组,我在其中添加了一些Card类型的对象,现在我需要从数组中删除这些对象,我使用以下代码:ios中的removeObject,ios,objective-c,Ios,Objective C,我的程序有一个名为“matchedCards”的NSMutable数组,我在其中添加了一些Card类型的对象,现在我需要从数组中删除这些对象,我使用以下代码: for (Card * removeCards in matchedCards) { [self.matchedCards removeObject:removeCards]; } 第一个卡片对象被删除,程序崩溃后,有人能解释一下原因吗?如果它成功删除了第一个对象,为什么它从第二个对象开始抛出错误在快速枚举数组时,不能从
for (Card * removeCards in matchedCards)
{
[self.matchedCards removeObject:removeCards];
}
第一个卡片对象被删除,程序崩溃后,有人能解释一下原因吗?如果它成功删除了第一个对象,为什么它从第二个对象开始抛出错误在快速枚举数组时,不能从数组中删除元素。
如果只想删除所有对象,请执行以下操作:
[self.matchedCards removeAllObjects];
但是,如果只想删除某些元素,请记住它们在索引集中的索引,然后删除它们
NSMutableIndexSet* indexesToRemove = [NSMutableIndexSet new];
for (NSUInteger index = 0; index < [self.matchedCards count]; ++index) {
if (whatever) {
[indexesToRemove addObject:index];
}
}
[self.matchedCards removeObjectsAtIndexes:indexesToRemove];
NSMutableIndexSet*indexesToRemove=[NSMutableIndexSet new];
用于(整数索引=0;索引<[self.matchedCards count];++索引){
如果(无论什么){
[indexesToRemove addObject:index];
}
}
[self.matchedCards RemoveObjectSatiIndex:indexesToRemove];
快速枚举数组时,不能从数组中删除元素。如果只想删除所有对象,请执行以下操作:
[self.matchedCards removeAllObjects];
但是,如果只想删除某些元素,请记住它们在索引集中的索引,然后删除它们
NSMutableIndexSet* indexesToRemove = [NSMutableIndexSet new];
for (NSUInteger index = 0; index < [self.matchedCards count]; ++index) {
if (whatever) {
[indexesToRemove addObject:index];
}
}
[self.matchedCards removeObjectsAtIndexes:indexesToRemove];
NSMutableIndexSet*indexesToRemove=[NSMutableIndexSet new];
用于(整数索引=0;索引<[self.matchedCards count];++索引){
如果(无论什么){
[indexesToRemove addObject:index];
}
}
[self.matchedCards RemoveObjectSatiIndex:indexesToRemove];
快速枚举数组时,不能从数组中删除元素。如果只想删除所有对象,请执行以下操作:
[self.matchedCards removeAllObjects];
但是,如果只想删除某些元素,请记住它们在索引集中的索引,然后删除它们
NSMutableIndexSet* indexesToRemove = [NSMutableIndexSet new];
for (NSUInteger index = 0; index < [self.matchedCards count]; ++index) {
if (whatever) {
[indexesToRemove addObject:index];
}
}
[self.matchedCards removeObjectsAtIndexes:indexesToRemove];
NSMutableIndexSet*indexesToRemove=[NSMutableIndexSet new];
用于(整数索引=0;索引<[self.matchedCards count];++索引){
如果(无论什么){
[indexesToRemove addObject:index];
}
}
[self.matchedCards RemoveObjectSatiIndex:indexesToRemove];
快速枚举数组时,不能从数组中删除元素。如果只想删除所有对象,请执行以下操作:
[self.matchedCards removeAllObjects];
但是,如果只想删除某些元素,请记住它们在索引集中的索引,然后删除它们
NSMutableIndexSet* indexesToRemove = [NSMutableIndexSet new];
for (NSUInteger index = 0; index < [self.matchedCards count]; ++index) {
if (whatever) {
[indexesToRemove addObject:index];
}
}
[self.matchedCards removeObjectsAtIndexes:indexesToRemove];
NSMutableIndexSet*indexesToRemove=[NSMutableIndexSet new];
用于(整数索引=0;索引<[self.matchedCards count];++索引){
如果(无论什么){
[indexesToRemove addObject:index];
}
}
[self.matchedCards RemoveObjectSatiIndex:indexesToRemove];
读取崩溃日志。它会说一些类似于
“枚举时声明了集合”
或者类似的
在使用for:in
循环对数组进行迭代时,不能对数组进行变异
但是你可以这样做
[matchedCards enumerateObjectsUsingBlock:^(Card *removedCards, NSInteger idx, BOOL *stop) {
[self.matchedCards removeObject:card];
}];
另外,使用当前代码,实际上是从
matchedCards
数组中删除所有对象。它将导致一个空数组。你确定这就是你想要的吗?阅读崩溃日志。它会说一些类似于
“枚举时声明了集合”
或者类似的
在使用for:in
循环对数组进行迭代时,不能对数组进行变异
但是你可以这样做
[matchedCards enumerateObjectsUsingBlock:^(Card *removedCards, NSInteger idx, BOOL *stop) {
[self.matchedCards removeObject:card];
}];
另外,使用当前代码,实际上是从
matchedCards
数组中删除所有对象。它将导致一个空数组。你确定这就是你想要的吗?阅读崩溃日志。它会说一些类似于
“枚举时声明了集合”
或者类似的
在使用for:in
循环对数组进行迭代时,不能对数组进行变异
但是你可以这样做
[matchedCards enumerateObjectsUsingBlock:^(Card *removedCards, NSInteger idx, BOOL *stop) {
[self.matchedCards removeObject:card];
}];
另外,使用当前代码,实际上是从
matchedCards
数组中删除所有对象。它将导致一个空数组。你确定这就是你想要的吗?阅读崩溃日志。它会说一些类似于
“枚举时声明了集合”
或者类似的
在使用for:in
循环对数组进行迭代时,不能对数组进行变异
但是你可以这样做
[matchedCards enumerateObjectsUsingBlock:^(Card *removedCards, NSInteger idx, BOOL *stop) {
[self.matchedCards removeObject:card];
}];
另外,使用当前代码,实际上是从
matchedCards
数组中删除所有对象。它将导致一个空数组。您确定这就是您想要的吗?在对数组进行迭代时,不能从数组中删除对象。改为这样做:
for (Card * removeCards in [matchedCards copy])
{
[self.matchedCards removeObject:removeCards];
}
在对数组进行迭代时,不能从数组中删除对象。改为这样做:
for (Card * removeCards in [matchedCards copy])
{
[self.matchedCards removeObject:removeCards];
}
在对数组进行迭代时,不能从数组中删除对象。改为这样做:
for (Card * removeCards in [matchedCards copy])
{
[self.matchedCards removeObject:removeCards];
}
在对数组进行迭代时,不能从数组中删除对象。改为这样做:
for (Card * removeCards in [matchedCards copy])
{
[self.matchedCards removeObject:removeCards];
}
原因是您正在删除当前对象并破坏
for语句
这里有一个解决方案:
for (int i = 0; i < self.matchedCards.count; i++)
{
if ([self.matchedCards[i] isKindOfClass:[YourClass class]])
{
[self.matchedCards removeObject:self.matchedCards[i]];
i--; // invalidate the removed index
}
}
NSLog(@"%@", self.matchedCards);
for(int i=0;i
请注意,i--
非常重要,否则您将无法访问数组的最后一个元素
希望这对你有帮助。。干杯。原因是您正在删除当前对象并破坏
for语句
这里有一个解决方案:
for (int i = 0; i < self.matchedCards.count; i++)
{
if ([self.matchedCards[i] isKindOfClass:[YourClass class]])
{
[self.matchedCards removeObject:self.matchedCards[i]];
i--; // invalidate the removed index
}
}
NSLog(@"%@", self.matchedCards);
for(int i=0;i
请注意,i--
非常重要,否则您将无法访问数组的最后一个元素
希望这对你有帮助。。干杯。原因是您正在删除当前对象并破坏
for语句
他