[英]Swift: Cannot convert value of type 'String' to expected argument type '@noescape (AnyObject) throws -> Bool'
I know this is a simple question, yet i am having problem finding the solutions. 我知道这是一个简单的问题,但是我在寻找解决方案时遇到了问题。
In Swift 2.2, I am getting this error. 在Swift 2.2中,出现此错误。
Cannot convert value of type 'String' to expected argument type '@noescape (AnyObject) throws -> Bool'
无法将“字符串”类型的值转换为预期的参数类型“ @noescape(AnyObject)引发-> Bool”
Here is the code. 这是代码。
var tableDataSectionsDict: CHOrderedDictionary?
if !tableDataSectionsDict!.allKeys.contains("Yesterday") {
tableDataSectionsDict!.insertObject(YesterdayArray, forKey: "Yesterday", atIndex: count)
}
What i am missing here ? 我在这里想念什么? Can anybody shade some light on it ?
有人可以在上面遮住一些光吗?
I'm assuming that CHOrderedDictionary
is this CHOrderedDictionary . 我假设
CHOrderedDictionary
是此CHOrderedDictionary 。 If that's the case, it is an Objective-C type that you have bridged to Swift (please correct me if I'm wrong). 如果是这种情况,那是您桥接到Swift的一种Objective-C类型(如果我错了,请纠正我)。
There are two contains
functions available: 有两个
contains
功能可用:
extension SequenceType where Generator.Element : Equatable {
/// Returns `true` iff `element` is in `self`.
@warn_unused_result
public func contains(element: Self.Generator.Element) -> Bool
}
extension SequenceType {
/// Returns `true` iff an element in `self` satisfies `predicate`.
@warn_unused_result
public func contains(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Bool
}
CHOrderedDictionary
's keys and values are converted from id
in Objective-C to AnyObject
in Swift. CHOrderedDictionary
的键和值从Objective-C中的id
转换为Swift中的AnyObject
。 AnyObject
does not conform to Equatable
, so you cannot use the first contains
function. AnyObject
不符合Equatable
,因此您不能使用第一个contains
函数。 You could proabably write a wrapper around CHOrderedDictionary
to allow some Equatable
element types if you really wanted to. 如果您确实
CHOrderedDictionary
,可以围绕CHOrderedDictionary编写包装,以允许某些Equatable
元素类型。
The fastest solution here will be to use the more generic version of contains
(the second contains
). 这里最快的解决方案是使用更通用的
contains
版本(第二个contains
)。 In code, it would look something like this: 在代码中,它看起来像这样:
// contains will iterate over the entire allKeys array. $0 is the key at each iteration.
let containsKey = tableDataSectionsDict!.allKeys.contains { return $0 as? String == "Yesterday" }
if !containsKey {
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.