Swift闭包:不能将类型'(_)-> Bool'的值转换为预期的参数类型

时间:2020-12-12 16:35:19

I'm working with the filter() method in Swift, but encountering a problem I can't seem to reproduce in a playground.

我正在使用Swift的filter()方法,但是遇到了一个在操场上无法重现的问题。

Edit: Uploaded an example project here: https://www.dropbox.com/s/5ce5uyxnpb0mndf/WeirdSwifty.zip?dl=0

编辑:在这里上传一个示例项目:https://www.dropbox.com/s/5ce5uyxnpb0mndf/weirdswifty.zip?

I have a Card struct which contains a CardType enum:

我有一个包含卡片类型enum的卡片结构:

struct Card {
    var name = ""
    var type : CardType

    enum CardType {
        case Red
        case Black
    }
}

And a Player class which maintains an array of these Card items:

以及一个维护这些卡片项的数组的Player类:

class Player {
    var hand : [Card]

    init() {
        hand = ...
    }

    func redCards() -> [Card] {
        return hand.filter({ (card) -> Bool in
            return card.type == .Red
        })
    }
}

However, Xcode is throwing an error no matter how I try to format this closure. I even let Xcode autocomplete the closure signature/body, thinking I had a syntax error, but it keeps recreating it the same (correct?) way:

但是,无论我如何格式化这个闭包,Xcode都会抛出一个错误。我甚至让Xcode自动完成闭包签名/正文,认为我有语法错误,但它仍然保持原样(正确?)

Swift闭包:不能将类型'(_)-> Bool'的值转换为预期的参数类型

I've also tried editing what Xcode automatically generates and providing a simpler version, to no avail:

我也尝试过编辑Xcode自动生成和提供一个更简单的版本,但没有用:

Swift闭包:不能将类型'(_)-> Bool'的值转换为预期的参数类型

Similarly:

类似的:

Swift闭包:不能将类型'(_)-> Bool'的值转换为预期的参数类型

As I mention I'm unable to reproduce this in a simple playground example, but I can't narrow down what's actually causing the issue in my primary project.

正如我所提到的,我无法在一个简单的操场示例中重现这个问题,但是我不能缩小在我的主要项目中真正造成问题的原因。

This is the only error in my project, and if I comment out the method containing the filter() call, it builds properly.

这是我的项目中惟一的错误,如果我注释掉包含filter()调用的方法,它就会正常构建。

Is this error a common red herring for some other actual issue?

对于其他实际问题来说,这个错误是一个常见的转移注意力的错误吗?

Note: Using Xcode 7.3.1 (7D1014)

注意:使用Xcode 7.3.1 (7D1014)

1 个解决方案

#1


17  

Something flakey is happing with Swift's type inference. Give card an explicit type and it will work.

flakey正在与Swift的类型推断发生关系。给card一个显式类型,它就会工作。

return hand.filter({ (card: Card) -> Bool in return card.type == .Red })

You don't need the return type or the return:

您不需要返回类型或返回:

return hand.filter({ (card: Card) in card.type == .Red })

Note: this works also:

注意:这个作品也:

return hand.filter({ ($0 as Card).type == .Red })

Fully specifying the .Red enum value resolves the issue as well:

充分指定.Red enum值也可以解决这个问题:

return hand.filter({ $0.type == Card.CardType.Red })

It was mentioned in the comments that if you move the definition of Card into the same file as the filter, that it works. In fact, if you split the definition of the CardType enum out from Card and just move CardType into the file with the filter, it works.

在评论中提到,如果将Card的定义移动到与过滤器相同的文件中,它就可以工作了。实际上,如果您将CardType enum的定义从Card中分离出来,然后使用过滤器将CardType移动到文件中,它就可以工作了。

#1


17  

Something flakey is happing with Swift's type inference. Give card an explicit type and it will work.

flakey正在与Swift的类型推断发生关系。给card一个显式类型,它就会工作。

return hand.filter({ (card: Card) -> Bool in return card.type == .Red })

You don't need the return type or the return:

您不需要返回类型或返回:

return hand.filter({ (card: Card) in card.type == .Red })

Note: this works also:

注意:这个作品也:

return hand.filter({ ($0 as Card).type == .Red })

Fully specifying the .Red enum value resolves the issue as well:

充分指定.Red enum值也可以解决这个问题:

return hand.filter({ $0.type == Card.CardType.Red })

It was mentioned in the comments that if you move the definition of Card into the same file as the filter, that it works. In fact, if you split the definition of the CardType enum out from Card and just move CardType into the file with the filter, it works.

在评论中提到,如果将Card的定义移动到与过滤器相同的文件中,它就可以工作了。实际上,如果您将CardType enum的定义从Card中分离出来,然后使用过滤器将CardType移动到文件中,它就可以工作了。