无法转换“Meme!"类型的值到预期的参数类型 '@noescape (Meme) throws ->布尔' [英] Cannot convert value of type 'Meme!' to expected argument type '@noescape (Meme) throws -> Bool'

查看:46
本文介绍了无法转换“Meme!"类型的值到预期的参数类型 '@noescape (Meme) throws ->布尔'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

    @IBAction func deleteMeme(sender: UIBarButtonItem) {
       if let foundIndex = MemeRepository.sharedInstance.memes.indexOf(selectedMeme) {     
        //remove the item at the found index
        MemeRepository.sharedInstance.memes.removeAtIndex(foundIndex)
        navigationController?.popViewControllerAnimated(true)

错误发生在 (selectedMeme).indexOf 方法中.

The error happens at the .indexOf method at (selectedMeme).

无法将 Meme! 类型的值转换为预期的参数类型 @noescape (Meme) throws ->布尔

Cannot convert value of type Meme! to expected argument type @noescape (Meme) throws -> Bool

模因!是我的应用程序的结构.我该如何解决这个问题?

Meme! is a struct for my app. How do I work through this?

struct Meme {

var topText : String!
var bottomText: String!
var image: UIImage!
var memedImage: UIImage!

init(topText: String, bottomText: String, image: UIImage, memedImage: UIImage) {
    self.topText = topText
    self.bottomText = bottomText
    self.image = image
    self.memedImage = memedImage

推荐答案

错误信息具有误导性.您真正需要的是为编译器提供一种方法来比较两个 Meme 实例并决定这些实例相等的标准.

The error message is misleading. What you actually need is to provide the compiler a way to compare two Meme instances and decide upon which criteria those instances are equal.

假设您希望将具有相同 name 属性的两个实例视为相等.

Let's say you want two instances having the same name property to be treated as equal.

我们使结构符合 Equatable 并且我们还创建了一个 == 运算符,通过它们的名称属性比较两个结构:

We make the struct conform to Equatable and we also create an == operator that compares two structs by their name property:

struct Meme:Equatable {
    var name:String!
}

func ==(lhs: Meme, rhs: Meme) -> Bool {
    return lhs.name == rhs.name
}

现在我们可以将 indexOf 与 Meme 实例一起使用:

Now we can use indexOf with a Meme instance:

let doge = Meme(name: "doge")

let lolcat = Meme(name: "lolcat")

let memes = [lolcat, doge]

if let dogeIndex = memes.indexOf(doge) {
    print(dogeIndex)  // 1
}

如果您不想通过它们的 name 属性而是通过它们的唯一性来比较两个实例,那么您必须使结构符合 Hashable 并使用唯一的 hashValue 属性返回一个 Int:

If you wanted to compare two instances not by their name property but by their uniqueness, then you would have to make the struct conform to Hashable and use a unique hashValue property returning an Int:

struct Meme:Hashable {
    var name:String!
    var hashValue: Int {
        return self.name.hashValue
    }
    init(name: String) {
        self.name = name
    }
}

func ==(lhs: Meme, rhs: Meme) -> Bool {
    return lhs.hashValue == rhs.hashValue
}

let doge = Meme(name: "doge")

let lolcat = Meme(name: "lolcat")

let memes = [lolcat, doge]

if let dogeIndex = memes.indexOf(doge) {
    print(dogeIndex)  // 1
}

在这个例子中,hashValue 是由 self.name 组成的,所以两个不同的 Meme 实例具有相同的 name 属性将被认为是相等的.如果您不想这样,请使用其他来源的哈希值.

In this example the hashValue is made from self.name, so two different instances of Meme with a same name property will be considered equal. If you don't want that, use another source for the hash value.

注意:在 Swift 3 中,indexOf 变成了 index(of:),所以对于这个例子,我们将改变 memes.indexOf(doge)memes.index(of: doge).

Note: in Swift 3, indexOf has become index(of:), so for this example we would change memes.indexOf(doge) to memes.index(of: doge).

这篇关于无法转换“Meme!"类型的值到预期的参数类型 '@noescape (Meme) throws ->布尔'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆