如何在Swift中使用indexesOfObjectsPassingTest:

编程入门 行业动态 更新时间:2024-10-15 22:31:32
本文介绍了如何在Swift中使用indexesOfObjectsPassingTest:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

indexesOfObjectsPassingTest的声明:在Swift中如下所示:

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

我试过各种排列方式让这个工作,但我很沮丧,特别是你如何处理这一块, CMutablePointer< ObjCBool​​>) - > Bool)!。我发现它很混乱,当我第一次学习块objective-c,如何从声明转换为实际使用的方法,并且Swift的语法至少是混乱。

<这个代码在我的工作在Playground;)希望它帮助一点

额外的功能 - 定义

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))//输出6(因为8%2 = 0)

Inline-Closure

与内联闭包(在Swift电子书中描述)。参数列表和返回类型由中的项分隔。

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 let number = object as Int return(number%2 == 0)// for even numbers }) println(\ lastIndex))//打印6(因为8%2 = 0)

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.

解决方案

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)

更多推荐

如何在Swift中使用indexesOfObjectsPassingTest:

本文发布于:2023-11-26 21:59:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Swift   indexesOfObjectsPassingTest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!