在Swift 3转换之后,我不能摆脱错误:“'对'indexOfObject(passingTest:)'的不确定使用' [英] After Swift 3 conversion, I can't get rid of error: "Ambiguous use of 'indexOfObject(passingTest:)'"
问题描述
我使用 NSArray
的indexesOfObjects(passingTest :),但是我把我的代码转换为Swift 3后,我得到的错误:ambiguous use of'indexOfObject passingTest :)'。
我的代码下面的工作正常与Swift 2.3。
let indexesOfBubbleConstraints = bubbleConstraints.indexesOfObjects(passingTest:{ idx,stop)in
if let view = constraint.firstItem as?UIView {
return view.tag == usernameTag
}
else {
return false
}
})
对于Swift 3, $ c> constraint 到 AnyObject
,但这不能解决实际的问题。
我最后使用 func indexesOfObjects(options:NSEnumerationOptions = [],passingTest:(Any,Int,UnsafeMutablePointer< ObjCBool>) - > Bool)
选项如下。这工作,但我仍然不明白为什么我得到的不明确...错误与我原来的实现。
let indexesOfBubbleConstraints = bubbleConstraints.indexesOfObjects(options:[],passingTest:{(约束,idx,停止)在
如果let view =(约束为AnyObject).firstItem as?UIView {
return view .tag == usernameTag
}
else {
return false
}
})
在Objective-C中,两个 indexesOf
方法有两种方法: / p>
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL(NS_NOESCAPE ^)(ObjectType obj,NSUInteger idx,BOOL * stop))谓词NS_AVAILABLE (10_6,4_0);
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL(NS_NOESCAPE ^)(ObjectType obj,NSUInteger idx,BOOL * stop))谓词NS_AVAILABLE(10_6,4_0);
现在,Swift 3导入那些不明确的两个方法:
@available(iOS 4.0,*)
open func indexesOfObjects(passingTest predicate:(Any,Int,UnsafeMutablePointer< ObjCBool>) - > Bool) > IndexSet
@available(iOS 4.0,*)
open func indexesOfObjects(options opts:NSEnumerationOptions = [],passingTest predicate:(Any,Int,UnsafeMutablePointer< ObjCBool>) - > Bool ) - > IndexSet
一个是 indexesOfObjects(passingTest:)
,另一个是 indexesOfObjects(options:passingTest:)
。不幸的是,Swift 3已经为参数 options
提供了一个默认值,它进行了一个简单的调用,如 bubbleConstraints.indexesOfObjects(passingTest:... )
含糊不清。
可能正在呼叫
-
indexesOfObjects(passingTest:)
或
-
indexesOfObjects(options:passingTest:)
,默认值为c $ c>
(Swift不应该给出默认值,如果它导致这种模糊性。 )
在这种情况下,您的解决方法代码使用 indexesOfObjects(options:passingTest:)
是另一个工作:
bubbleConstraints.indexesOfObjects(passingTest :) {constraint,idx,stop in
//。 ..
}
方法引用 .indexesOfObjects(passingTest: )
返回方法 indexesOfObjects(passingTest:)
作为闭包,上面的表达式调用它。
$顺便说一句,你最好考虑使用Swift的收集方法,而不是使用一个
NSArray
s方法:b $ b <
let indexesOfBubbleConstraints = bubbleConstraints.enumerated()。lazy
.filter {(idx,constraint)in
// ...
} .map {$ 0.offset}
I'm using NSArray
's indexesOfObjects(passingTest:), but after I converted my code to Swift 3 I get the error: "Ambiguous use of 'indexOfObject(passingTest:)'".
My code below worked fine with Swift 2.3.
let indexesOfBubbleConstraints = bubbleConstraints.indexesOfObjects(passingTest: { (constraint, idx, stop) in
if let view = constraint.firstItem as? UIView{
return view.tag == usernameTag
}
else{
return false
}
})
For Swift 3, I also had to cast constraint
to AnyObject
, but that doesn't fix the actual problem.
I ended up using func indexesOfObjects(options: NSEnumerationOptions = [], passingTest: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool)
with an empty Array for options as below. This works, but I still don't understand why I'm getting the "Ambiguous..." error with my original implementation.
let indexesOfBubbleConstraints = bubbleConstraints.indexesOfObjects(options: [], passingTest: { (constraint, idx, stop) in
if let view = (constraint as AnyObject).firstItem as? UIView{
return view.tag == usernameTag
}
else{
return false
}
})
In Objective-C, the two indexesOf
methods are distinct two methods:
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
And now, Swift 3 import those as ambiguous two methods:
@available(iOS 4.0, *)
open func indexesOfObjects(passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet
@available(iOS 4.0, *)
open func indexesOfObjects(options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet
One is indexesOfObjects(passingTest:)
, and another is indexesOfObjects(options:passingTest:)
. And unfortunately, Swift 3 has given a default value for the parameter options
, which has made a simple call like bubbleConstraints.indexesOfObjects(passingTest: ...)
ambiguous.
It may be calling
indexesOfObjects(passingTest:)
or
indexesOfObjects(options:passingTest:)
with giving default value tooptions
(Swift should not give a default value, if it causes this sort of ambiguity. Better send a bug report.)
In this case, your workaround code, using indexesOfObjects(options:passingTest:)
should work, but there is another work around:
bubbleConstraints.indexesOfObjects(passingTest:) {constraint, idx, stop in
//...
}
The method reference .indexesOfObjects(passingTest:)
returns the method indexesOfObjects(passingTest:)
as a closure, and the above expression is calling it.
By the way, you better consider using Swift's collection methods, rather than using an NSArray
s method:
let indexesOfBubbleConstraints = bubbleConstraints.enumerated().lazy
.filter {(idx, constraint) in
//...
}.map{$0.offset}
这篇关于在Swift 3转换之后,我不能摆脱错误:“'对'indexOfObject(passingTest:)'的不确定使用'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!