Bool "" /> Bool " - 秒客网" />

不能转换类型'Meme '的值!" to expected argument type " @noescape (Meme) throw -> Bool "

时间:2020-11-30 18:03:53

Here is the code:

这是代码:

    @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)

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

错误发生在。indexof方法at (selectedMeme)。

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

不能转换类型模因的值!对期望参数类型@noescape (Meme)抛出-> Bool

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

Meme !是我的应用程序的结构体。我如何通过它工作?

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

2 个解决方案

#1


44  

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.

错误消息具有误导性。实际上,您需要的是为编译器提供一种方法来比较两个模因实例,并确定这些实例是相等的。

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

假设您希望两个具有相同名称属性的实例被视为相等的。

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

我们使结构符合Equatable,并创建一个==运算符,根据两个结构的名称属性对它们进行比较:

struct Meme:Equatable {
    var name:String!
}

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

Now we can use indexOf with a Meme instance:

现在我们可以使用indexOf和一个模因实例:

let doge = Meme(name: "doge")

let lolcat = Meme(name: "lolcat")

let memes = [lolcat, doge]

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

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:

如果要比较两个实例,不是根据它们的名称属性,而是根据它们的唯一性,那么必须使结构符合Hashable,并使用一个返回Int的惟一hashValue属性:

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
}

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.

在本例中,hashValue是由self.name创建的,因此将认为具有相同名称属性的两个不同的Meme实例是相等的。如果不需要,可以使用另一个源作为散列值。

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

注意:在Swift 3中,indexOf已成为index(of:),因此在本例中,我们将memes.indexOf(doge)改为memes。指数(:总督)。

#2


5  

If you want to put the comparison inside the indexOf method itself, do it like this:

如果您想将比较放在indexOf方法本身中,请这样做:

if let foundIndex = MemeRepository.sharedInstance.memes.indexOf({
  UIImagePNGRepresentation($0.memedImage) == UIImagePNGRepresentation(selectedMeme.memedImage)})

Probably not the best way to compare images. If you know the images are the same object, you can use:

可能不是比较图像的最好方法。如果你知道图像是相同的对象,你可以使用:

.indexOf({$0.memedImage == selectedMeme.memedImage})

but if you want to compare them pixel by pixel or compare the same image scaled to different sizes, that is a little more complicated.

但是如果你想对它们逐像素进行比较,或者将同一幅图像缩放到不同大小,那就有点复杂了。

#1


44  

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.

错误消息具有误导性。实际上,您需要的是为编译器提供一种方法来比较两个模因实例,并确定这些实例是相等的。

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

假设您希望两个具有相同名称属性的实例被视为相等的。

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

我们使结构符合Equatable,并创建一个==运算符,根据两个结构的名称属性对它们进行比较:

struct Meme:Equatable {
    var name:String!
}

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

Now we can use indexOf with a Meme instance:

现在我们可以使用indexOf和一个模因实例:

let doge = Meme(name: "doge")

let lolcat = Meme(name: "lolcat")

let memes = [lolcat, doge]

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

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:

如果要比较两个实例,不是根据它们的名称属性,而是根据它们的唯一性,那么必须使结构符合Hashable,并使用一个返回Int的惟一hashValue属性:

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
}

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.

在本例中,hashValue是由self.name创建的,因此将认为具有相同名称属性的两个不同的Meme实例是相等的。如果不需要,可以使用另一个源作为散列值。

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

注意:在Swift 3中,indexOf已成为index(of:),因此在本例中,我们将memes.indexOf(doge)改为memes。指数(:总督)。

#2


5  

If you want to put the comparison inside the indexOf method itself, do it like this:

如果您想将比较放在indexOf方法本身中,请这样做:

if let foundIndex = MemeRepository.sharedInstance.memes.indexOf({
  UIImagePNGRepresentation($0.memedImage) == UIImagePNGRepresentation(selectedMeme.memedImage)})

Probably not the best way to compare images. If you know the images are the same object, you can use:

可能不是比较图像的最好方法。如果你知道图像是相同的对象,你可以使用:

.indexOf({$0.memedImage == selectedMeme.memedImage})

but if you want to compare them pixel by pixel or compare the same image scaled to different sizes, that is a little more complicated.

但是如果你想对它们逐像素进行比较,或者将同一幅图像缩放到不同大小,那就有点复杂了。