斯威夫特:不能转换类型的值使用过滤功能来@noescape同时通话的indexOf,过滤后 [英] Swift: cannot convert value of type to @noescape while call indexOf, after filter using filter function

查看:194
本文介绍了斯威夫特:不能转换类型的值使用过滤功能来@noescape同时通话的indexOf,过滤后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在斯威夫特2,我收到了一个错误:


  

无法将类型'[字符串:AnyObject]'的值来预期的参数类型'@ noescape([字符串:AnyObject])抛出 - > BOOL


  //今天= NSDate的()
//数组:[[字符串:AnyObject]
//我用的,如果我们是因为我们现在可以在阵列中得到元件匹配我们的条件如果让elementOfArray = array.filter({$ 0身份证]为int == anotherDictionary [matchID]为int&放大器;?&放大器;($ 0nextFireDate]作为NSDate的)?比较(今天)? == NSComparisonResult.OrderedAscending})。第一个{   让指数=​​ array.indexOf(elementOfArray)//这里的错误
}

我在做什么错?我无法理解。 :/

我的目标,是要找到该项目的指标,我认为我打开替代解决方案,但当然这一次是preferred,因为我认为这是正确的方式。


解决方案

在斯威夫特阵列的的indexOf 方法并不需要一个类型匹配数组类型的对象。相反,它需要一个闭合。那闭包接受阵列的类型的元素,并返回一个布尔值。

所以,其实,我们没有(也不应该)甚至与过滤器来电打扰,除非我们真正需要得到的阵列。如果我们只是在寻找传递的第一个对象的任何测试你过滤了......嗯,我们只是传递一个相同的测试,以的indexOf

所以,让事情变得简单,如果我们有一个字符串数组(让我们说,他们有很多重复的所有单字母串),我想找到字符串一,而不是过滤数组到字符串是,然后发现,通过该测试的第一个字符串第一方法,然后发现确切的对象的索引,而不是,我只是通过这个测试到的indexOf 方法

 让字母:[字符串] = [X,Y,Z,A,B,C]让指数=​​ {letters.indexOf
    $ 0 ==A
}


为了清楚起见,似乎只是传递的单个元素,并寻找在某些情况下确实工作。它可能是依赖于符合斯威夫特的 Equatable 的协议。我可以例如具有简单求使用 letters.indexOf(A)在这里。编译器会很高兴。但显然,并非每一个数组由需要装东西符合 Equatable ,并且阵列无法就如何再比较其内容的假设。在这些情况下,将不得不使用传递闭合的上述例子。这也可能是值得注意的是,封闭传递给的indexOf ,而不是先过滤,然后调用的indexOf 将是异乎寻常更多高效无论如何,即使你的阵列允许在 letters.indexOf(A)办法。例如,如果,我有更复杂的字符串,我只是想以字母A开始的第一个字符串,这将是远远超过了原始数组渗透到字符串数组开始启动高效一个

In Swift 2, I'm receiving an error:

Cannot convert value of type '[String:AnyObject]' to expected argument type '@noescape ([String:AnyObject])' throws -> Bool"

//today = NSDate()
//array : [[String:AnyObject]]
// I use if let because of we might now get element in the array matching our condition

if let elementOfArray = array.filter({$0["id"] as? Int == anotherDictionary["matchID"] as? Int && ($0["nextFireDate"] as? NSDate)?.compare(today) == NSComparisonResult.OrderedAscending}).first {

   let index = array.indexOf(elementOfArray) // error here
}

What I'm doing wrong? I can't understand. :/

My aim, is to find index of that item, I think that I open for alternative solutions, but of course this one is preferred, because I think this is the "right way".

解决方案

The indexOf method on Swift arrays does not take an object of a type matching the array's type. Instead, it takes a closure. That closure takes an element of the array's type and returns a bool.

So, in fact, we don't (and shouldn't) even bother with the filter call unless we actually need the resultant array. If we're just looking for the first object that passes whatever test you are filtering for... well we just pass that exact same test to indexOf.

So, to keep things simple, if we have an array of strings (and let's say they're all single letter strings with lots of repetition), and I want to find the first index of the string "a", rather than filtering the array down to strings that are "a", then finding the first string that passed that test with the first method, and then finding the index of that exact object, instead, I just pass that test into the indexOf method:

let letters: [String] = ["x", "y", "z", "a", "b", "c"]

let index = letters.indexOf {
    $0 == "a"
}


For clarity, it appears that simply passing an individual element and looking for that does work in some cases. It probably relies on conformance to Swift's Equatable protocol. I could for example have simplied used letters.indexOf("a") here. The compiler would have been happy. But obviously, not every array is composed required to hold things that conform to Equatable, and the array can't make assumptions about how to compare its elements then. In these cases, you will have to use the above example of passing a closure. It's probably worth noting that passing this closure to indexOf rather than first filtering and then calling indexOf is going to be egregiously more efficient anyway, even if your array allows the letters.indexOf("a") approach. If for example, I had more complex strings, and I just wanted the first string that started with the letter 'a', this would be far, far more efficient than starting by filtering down the original array to an array of strings starting with 'a'.

这篇关于斯威夫特:不能转换类型的值使用过滤功能来@noescape同时通话的indexOf,过滤后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆