Swift中的NSFastEnumeration [英] NSFastEnumeration in Swift
问题描述
我正在尝试将Objective-C项目转换为swift,但我无法找到如何将NSFastEnumeration用于符合NSFastEnumeration的类的对象。
I am trying to convert an Objective-C project to swift, but I am unable to find how to use NSFastEnumeration for an object of a class that conforms to NSFastEnumeration.
以下是ObjC中的代码:
Here is the code in ObjC:
// get the decode results
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
// just grab the first barcode
break;
到目前为止,我试图找到如何做到这一点,但这似乎不起作用,这里是swift代码:
so far I tried to find how to do this, but this doe not seems work, here is the swift code:
var results: ZBarSymbolSet = infoDictionary?.objectForKey(ZBarReaderControllerResults) as ZBarSymbolSet
var symbol : ZBarSymbol? = nil;
for symbol in results
{ //just grab first barcode
break;
}
条件错误 - ZBarSymbolSet没有名为的成员发电机
the error comes in for condition - "ZBarSymbolSet" does not have a member named "Generator"
我做错了什么?
这是屏幕截图
Here is the screen shot
推荐答案
经过一段时间在swift框架文件周围探索,我终于找到了这个名为 NSFastGenerator
的好类。 NSSet
和朋友似乎使用相同的生成器
。
After a while poking around the swift framework files, I finally found this nice class called NSFastGenerator
. NSSet
and friends seem to be using the same Generator
.
对于 ZBarSymbolSet
,以下是如何将其扩展为支持 for-in
循环:
For ZBarSymbolSet
, here's how you'd extend it to support for-in
loops:
extension ZBarSymbolSet: SequenceType {
public func generate() -> NSFastGenerator {
return NSFastGenerator(self)
}
}
更新:看起来Swift 2.0的协议扩展为我们解决了这个问题!
Update: Looks like Swift 2.0's protocol extensions fixed this for us!
这篇关于Swift中的NSFastEnumeration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!