Swift:无法将类型 'String' 的值转换为预期的参数类型“@noescape (AnyObject) throws -> Bool”
Swift: Cannot convert value of type 'String' to expected argument type '@noescape (AnyObject) throws -> Bool'
我知道这是一个简单的问题,但我在寻找解决方案时遇到问题。
在 Swift 2.2 中,我遇到了这个错误。
Cannot convert value of type 'String' to expected argument type
'@noescape (AnyObject) throws -> Bool'
这是代码。
var tableDataSectionsDict: CHOrderedDictionary?
if !tableDataSectionsDict!.allKeys.contains("Yesterday") {
tableDataSectionsDict!.insertObject(YesterdayArray, forKey: "Yesterday", atIndex: count)
}
我在这里缺少什么?有人可以给它遮光吗?
我假设 CHOrderedDictionary
是 this CHOrderedDictionary。如果是这种情况,它是一个 Objective-C 类型,你已经桥接到 Swift (如果我错了请纠正我)。
有两个 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
的键和值从Objective-C中的id
转换为Swift中的AnyObject
。 AnyObject
不符合Equatable
,所以不能使用第一个contains
函数。如果你真的想的话,你可以围绕 CHOrderedDictionary
编写一个包装器以允许一些 Equatable
元素类型。
此处最快的解决方案是使用更通用的 contains
版本(第二个 contains
)。在代码中,它看起来像这样:
// contains will iterate over the entire allKeys array. [=11=] is the key at each iteration.
let containsKey = tableDataSectionsDict!.allKeys.contains { return [=11=] as? String == "Yesterday" }
if !containsKey {
//...
}
我知道这是一个简单的问题,但我在寻找解决方案时遇到问题。
在 Swift 2.2 中,我遇到了这个错误。
Cannot convert value of type 'String' to expected argument type '@noescape (AnyObject) throws -> Bool'
这是代码。
var tableDataSectionsDict: CHOrderedDictionary?
if !tableDataSectionsDict!.allKeys.contains("Yesterday") {
tableDataSectionsDict!.insertObject(YesterdayArray, forKey: "Yesterday", atIndex: count)
}
我在这里缺少什么?有人可以给它遮光吗?
我假设 CHOrderedDictionary
是 this CHOrderedDictionary。如果是这种情况,它是一个 Objective-C 类型,你已经桥接到 Swift (如果我错了请纠正我)。
有两个 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
的键和值从Objective-C中的id
转换为Swift中的AnyObject
。 AnyObject
不符合Equatable
,所以不能使用第一个contains
函数。如果你真的想的话,你可以围绕 CHOrderedDictionary
编写一个包装器以允许一些 Equatable
元素类型。
此处最快的解决方案是使用更通用的 contains
版本(第二个 contains
)。在代码中,它看起来像这样:
// contains will iterate over the entire allKeys array. [=11=] is the key at each iteration.
let containsKey = tableDataSectionsDict!.allKeys.contains { return [=11=] as? String == "Yesterday" }
if !containsKey {
//...
}