How to use indexesOfObjectsPassingTest: in Swift

柔情痞子 提交于 2020-01-02 03:49:25

问题


The declaration of indexesOfObjectsPassingTest: looks like this in Swift,

func indexesOfObjectsPassingTest(predicate: ((AnyObject!, Int, CMutablePointer<ObjCBool>) -> Bool)!) -> NSIndexSet!

I've tried all sorts of permutations to get this to work, but I'm stumped, particularly with how you deal with this piece, CMutablePointer<ObjCBool>) -> Bool)!. I found it confusing enough, when I was first learning blocks in objective-c, how you translate from the declaration to actual use of a method, and the syntax of Swift is at least as confusing.


回答1:


This code is working in the Playground for me ;) Hope it helps a bit

Extra Function-Definition

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)

Inline-Closure

I transformed my above example to work with an inline-closure (described in the Swift-eBook). The parameter-list and return-type is separated by the term in.

import Cocoa

let list = [2, 3, 4, 5, 6, 7, 8]

let result: NSIndexSet = (list as NSArray).indexesOfObjectsPassingTest({
(object: AnyObject!, index: Int, 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)



回答2:


Seems like CMutablePointer was recently changed to UnsafeMutablePointer.

This one worked for me:

let indices = myArray.indexesOfObjectsPassingTest({
    (obj: AnyObject!, idx: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Bool in
    var didPass = false
    //determine if obj passes test, if it does set didPass = true
    return didPass
})



回答3:


The block syntax is being replaced in Swift by a closure, the predicate parameter here.

So you can just do:

var array = NSArray(array: [1, 2, 3, 4, 5])

var indexSet = array.indexesOfObjectsPassingTest({ (val: AnyObject!, index: Int, stop: CMutablePointer<ObjCBool>) -> Bool in return index > 2 })

indexSet.count

Out of the method, it could look like this:

var closure = {(val: AnyObject!, index: Int, stop: CMutablePointer<ObjCBool>) -> Bool? in
    /* Do Something */
}

Hope this helps.



来源:https://stackoverflow.com/questions/24041842/how-to-use-indexesofobjectspassingtest-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!