indexOfObjectsPassingTest的声明:在
Swift中看起来像这样
func indexesOfObjectsPassingTest(predicate: ((AnyObject!,Int,CMutablePointer<ObjCBool>) -> Bool)!) -> NSIndexSet!
我已经尝试了各种各样的排列来让它工作,但我很难过,特别是你如何处理这件作品,CMutablePointer< ObjCBool>) – >布尔)!我发现它很混乱,当我第一次在objective-c中学习块时,你如何从声明转换为实际使用方法,而Swift的语法至少是令人困惑的.
解决方法
这段代码在我的游乐场工作;)希望它有点帮助
额外功能定义
import Cocoa let list = [2,3,4,5,6,7,8] func test (object: AnyObject!,index: Int,stop: CMutablePointer<ObjCBool>) -> Bool { let number = object as Int return (number % 2 == 0) //for even numbers } let result: NSIndexSet = (list as NSArray).indexesOfObjectsPassingTest(test) println("\(result.lastIndex)") //prints "6" (because 8%2=0)
直列关闭
我将上面的示例转换为内联闭包(在Swift-eBook中描述).参数列表和返回类型由术语in分隔.
import Cocoa let list = [2,8] let result: NSIndexSet = (list as NSArray).indexesOfObjectsPassingTest({ (object: AnyObject!,stop: CMutablePointer<ObjCBool>) -> Bool in let number = object as Int return (number % 2 == 0) //for even numbers }) println("\(result.lastIndex)") //prints "6" (because 8%2=0)