简体   繁体   English

iOS:修改NSFastEnumerationState以在枚举时隐藏变异

[英]iOS : Modifying NSFastEnumerationState to hide mutation while enumerating

I have a case where in my collections will be mutated while enumerating.But this is not allowed in iOS.On further investigation found that the NSFastEnumerationState is ac type struct as follows 我有一个案例,我的集合将在枚举时发生变异。但这在iOS中是不允许的。进一步调查发现NSFastEnumerationState是ac类型结构如下

typedef struct {
        unsigned long state;
        id *itemsPtr;
        unsigned long *mutationsPtr;
        unsigned long extra[5];
    } NSFastEnumerationState;

So state->mutationPtr denotes whether the collection was mutated or not,based on which exceptions are thrown.Can we override this to show that the collection is not mutated(even if it is mutated),So that the exceptions are not thrown.By overriding we can achieve mutation while enumeration.I don know whether this is a correct idea.Please let me know your suggestions. 所以state-> mutationPtr表示集合是否被突变,基于抛出的异常。我们可以覆盖它以显示集合没有变异(即使它被变异),因此不会抛出异常。通过压倒我们可以在枚举的同时实现变异。我不知道这是否是一个正确的想法。请让我知道你的建议。

The idea is incorrect, just copy the object you are enumerating and enumerate the copy. 这个想法是不正确的,只需复制您枚举的对象并枚举副本。

Edit : To address your comments, it seems that it's technically possible to achieve what you want. 编辑 :为了解决您的意见,似乎在技术上可以实现您想要的。 First thing I did is to go to the NSEnumerator documentation to check if you could basically create your own enumerator and implement a custom nextObject method to resolve the problem. 我做的第一件事是转到NSEnumerator文档来检查你是否可以基本上创建自己的枚举器并实现自定义的nextObject方法来解决问题。 This is what I found: 这是我发现的:

Note: It is not safe to modify a mutable collection while enumerating through it. 注意:在枚举可变集合时,修改它是不安全的。 Some enumerators may currently allow enumeration of a collection that is modified, but this behavior is not guaranteed to be supported in the future. 某些枚举器当前可能允许枚举已修改的集合,但不保证将来支持此行为。

Additionally, I checked the Enumeration: Traversing a Collection's Elements - Using an Enumerator section and I found a very similar thing: 另外,我检查了Enumeration:遍历集合的元素 - 使用枚举器部分 ,我发现了一个非常类似的东西:

It is not safe to remove, replace, or add to a mutable collection's elements while enumerating through it. 在枚举可变集合的元素时,删除,替换或添加它是不安全的。 If you need to modify a collection during enumeration, you can either make a copy of the collection and enumerate using the copy or collect the information you require during the enumeration and apply the changes afterwards. 如果需要在枚举期间修改集合,则可以复制集合并使用副本枚举或在枚举期间收集所需的信息,然后应用更改。

So if you sum this all up, it seems to me that you have 2 options: 所以,如果你总结一下,在我看来你有两个选择:

  1. Create your custom NSEnumerator subclass and use it to do what you need. 创建自定义NSEnumerator子类并使用它来执行您需要的操作。
  2. Don't use NSFastEnumeration at all and find another way to do what you need. 根本不要使用NSFastEnumeration ,并找到另一种方法来做你需要的。

http://www.mikeash.com/pyblog/friday-qa-2011-10-14-whats-new-in-gcd.html 。这个链接提供了一些关于这个问题的好信息。它在一定程度上帮助了我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM