简体   繁体   English

Swift:无法将'String'类型的值转换为预期的参数类型'@noescape(AnyObject)引发-> Bool'

[英]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.

相关问题 无法将&#39;Option&#39;类型的值转换为预期的参数类型&#39;@noescape(Option)throws - &gt; Bool&#39; - Cannot convert value of type 'Option' to expected argument type '@noescape (Option) throws -> Bool' Swift类别:“无法将类型的值转换为预期的参数类型&#39;AnyObject!&#39; - Swift category: "Cannot convert value of type to expected argument type 'AnyObject!' Swift 2无法将'[AnyObject]'类型的值转换为预期的参数类型'[UIViewController]?' - Swift 2 Cannot convert value of type '[AnyObject]' to expected argument type '[UIViewController]?' 无法将“NSMutableDictionary”类型的值转换为预期的参数类型“[String: AnyObject]?” - Cannot convert value of type 'NSMutableDictionary' to expected argument type '[String: AnyObject]?' 无法将“String”类型的值转换为预期的参数类型“AnyObject?” - Cannot convert value of type 'String' to expected argument type 'AnyObject?' 无法转换类型[[String:AnyObject] ?. Type”(也称为“可选” <Dictionary<String, AnyObject> &gt; .Type)到期望的参数类型 - Cannot convert value of type '[String : AnyObject]?.Type' (aka 'Optional<Dictionary<String, AnyObject>>.Type) to expected argument type Swift:无法使用类型为(((AnyObject)throws-&gt; Bool)&#39;的参数列表调用&#39;filter&#39; - Swift: Cannot invoke 'filter' with an argument list of type '((AnyObject) throws -> Bool)' 无法将类型'(key:String,value:AnyObject)'的值转换为预期的参数类型'[String:AnyObject]' - Cannot convert value of type '(key: String, value: AnyObject)' to expected argument type '[String : AnyObject]' Swift:无法将类型&#39;() - &gt; Bool&#39;的值转换为预期的参数类型&#39;PFObject&#39; - Swift: Cannot convert value of type '() -> Bool' to expected argument type 'PFObject' 无法将类型()的值转换为预期的参数类型bool(Swift)? - Cannot convert value of type () to expected argument type bool (Swift)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM