[英]Cannot convert value of type 'Option' to expected argument type '@noescape (Option) throws -> Bool'
I have a class called Option
. 我有一个名为
Option
的类。 This is a Realm object so it's a subclass of Realm's own base class Object
. 这是一个Realm对象,因此它是Realm自己的基类
Object
的子类。
In a view controller I have an array property that holds a bunch of Option
objects. 在视图控制器中,我有一个数组属性,它包含一堆
Option
对象。
private var options = [Option]()
In a method down the view controller, I need to check if a certain Option
object is contained within the aforementioned options array. 在视图控制器中的方法中,我需要检查上述选项数组中是否包含某个
Option
对象。
Previously (Swift 1.2), I have the checking logic like this. 以前(Swift 1.2),我有这样的检查逻辑。
func itemSelected(selection: Object) {
let option = selection as! Option
if !contains(options, option) {
// act accordingly
}
}
Now I'm converting the project to Swift 2 (I have updated the Realm's version to Swift 2 version as well). 现在我将项目转换为Swift 2(我已经将Realm的版本更新为Swift 2版本)。 I updated the code to this.
我将代码更新为此。
func itemSelected(selection: Object) {
let option = selection as! Option
if !options.contains(option) {
// act accordingly
}
}
But now I'm getting the following compile error! 但是现在我得到以下编译错误!
Cannot convert value of type 'Option' to expected argument type '@noescape (Option) throws -> Bool' 无法将'Option'类型的值转换为预期的参数类型'@noescape(Option)throws - > Bool'
I can't figure out why. 我无法弄清楚为什么。 Any ideas?
有任何想法吗?
This is because the contains
function now expects a closure rather than an element on all non-equatable types. 这是因为
contains
函数现在需要闭包而不是所有非等同类型的元素。 All you need to do is change it to the following 您需要做的就是将其更改为以下内容
if !(options.contains{$0==option}) {
// act accordingly
}
What this does is it passes a closure in to the function, which returns true only if that closure satisfies any of its elements. 它的作用是将闭包传递给函数,只有当闭包满足其任何元素时才返回true。
$0
stands for the current element in the array that the contains
function is testing against, and it returns true if that element is equal to the one that you are looking for. $0
代表contains
函数正在测试的数组中的当前元素,如果该元素等于您要查找的元素,则返回true。
While the first answer, that indicates this issue occurs because the contains
method needs to operate on an Equatable
type, is true, that's only half the story. 虽然第一个答案,表明出现此问题是因为
contains
方法需要在Equatable
类型上运行,这是真的,这只是故事的一半。 The Realm Object
class inherits NSObject
, which conforms to Equatable
(thus this should work without a closure). Realm
Object
类继承NSObject
,它符合Equatable
(因此这应该没有闭包)。 For more discussion on this, you can refer to this issue on the Realm GitHub page: https://github.com/realm/realm-cocoa/issues/2519 . 有关此问题的更多讨论,您可以在Realm GitHub页面上参考此问题: https : //github.com/realm/realm-cocoa/issues/2519 。 The Realm developers indicate that they believe this is a bug in Swift.
Realm开发人员表示他们认为这是Swift中的一个错误。
Ultimately, the suggested workaround is to re-declare the conformance to Equatable
and Hashable
, like so (this is copied verbatim from GitHub user bdash's comment on the previously posted issue): 最终,建议的解决方法是重新声明与
Equatable
和Hashable
的一致性,就像这样(这是从GitHub用户bdash对先前发布的问题的评论中逐字复制的):
public class A: Object, Equatable, Hashable {
}
public func ==(lhs: A, rhs: A) -> Bool {
return lhs.isEqual(rhs)
}
You'd replace the all instances of type A
with type Option
in that sample. 您将在该示例中使用类型
Option
替换类型A
的所有实例。
I've tested this solution, and it works for me in XCode 7.2.1, using Swift version 2.1.1. 我已经测试了这个解决方案,它适用于XCode 7.2.1,使用Swift版本2.1.1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.