如何将NSMutableOrderedSet转换为通用数组?

[英]How to covert NSMutableOrderedSet to generic array?


I have this for loop, p is a NSManagedObject, fathers is a to-many relationship, so I need to cast NSMutableOrderedSet to [Family] but it does not work, why?

我有这个for循环,p是一个NSManagedObject,父亲是一个多对多的关系,所以我需要将NSMutableOrderedSet转换为[Family],但它不起作用,为什么?

for f in p.fathers as [Family] {
}

enter image description here enter image description here

2 个解决方案

#1


20  

You can obtain an array representation of the set via the array property - then you can downcast it to the proper type and assign to a variable:

您可以通过数组属性获取集合的数组表示 - 然后您可以将其向下转换为正确的类型并分配给变量:

let families = p.fathers.array as [Family]

but of course you can also use it directly in the loop:

但当然你也可以直接在循环中使用它:

for f in p.fathers.array as [Family] {
    ....
}

#2


2  

The simple solution by Antonio should be used in this case. I'd just like to discuss this a bit more. If we try to enumerate an instance of 'NSMutableOrderedSet' in a 'for' loop, the compiler will complain:

安东尼奥的简单解决方案应该用于这种情况。我想再讨论一下这个问题。如果我们尝试在'for'循环中枚举'NSMutableOrderedSet'的实例,编译器会抱怨:

error: type 'NSMutableOrderedSet' does not conform to protocol 'SequenceType'

错误:类型'NSMutableOrderedSet'不符合协议'SequenceType'

When we write

我们写的时候

for element in sequence {
    // do something with element
}

the compiler rewrites it into something like this:

编译器将其重写为以下内容:

var generator = sequence.generate()

while let element = generator.next() {
    // do something with element
}

'NS(Mutable)OrderedSet' doesn't have 'generate()' method i.e. it doesn't conform to the 'SequenceType' protocol. We can change that. First we need a generator:

'NS(Mutable)OrderedSet'没有'generate()'方法,即它不符合'SequenceType'协议。我们可以改变这一点。首先我们需要一个发电机:

public struct OrderedSetGenerator : GeneratorType {

    private let orderedSet: NSMutableOrderedSet

    public init(orderedSet: NSOrderedSet) {
        self.orderedSet = NSMutableOrderedSet(orderedSet: orderedSet)
    }

    mutating public func next() -> AnyObject? {
        if orderedSet.count > 0 {
            let first: AnyObject = orderedSet.objectAtIndex(0)
            orderedSet.removeObjectAtIndex(0)
            return first
        } else {
            return nil
        }
    }
}

Now we can use that generator to make 'NSOrderedSet' conform to 'SequenceType':

现在我们可以使用该生成器使'NSOrderedSet'符合'SequenceType':

extension NSOrderedSet : SequenceType {
    public func generate() -> OrderedSetGenerator {
        return OrderedSetGenerator(orderedSet: self)
    }
}

'NS(Mutable)OrderedSet' can now be used in a 'for' loop:

'NS(Mutable)OrderedSet'现在可用于'for'循环:

let sequence = NSMutableOrderedSet(array: [2, 4, 6])

for element in sequence {
    println(element) // 2 4 6
}

We could further implement 'CollectionType' and 'MutableCollectionType' (the latter for 'NSMutableOrderedSet' only) to make 'NS(Mutable)OrderedSet' behave like Swift's standard library collections.

我们可以进一步实现'CollectionType'和'MutableCollectionType'(后者仅用于'NSMutableOrderedSet')以使'NS(Mutable)OrderedSet'表现得像Swift的标准库集合。

Not sure if the above code follows the best practises as I'm still trying to wrap my head around details of all these protocols.

不确定上面的代码是否遵循最佳实践,因为我仍然试图围绕所有这些协议的细节。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/11/22/e2f0cc5aef25a0c4875535bb8c1ad881.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号