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

2

27

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

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
Trophoplasm answered 5/10, 2015 at 15:34 Comment(0)
L
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:

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:

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:

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.

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

Labarum answered 5/10, 2015 at 16:13 Comment(0)
E
5

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

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.

Enfeeble answered 21/10, 2015 at 4:49 Comment(1)
Thank you all for your input and help. I appreciate the time and effort. Here's my project if you want to check it out: github.com/pbellot77/MemeMe.gitTrophoplasm

© 2022 - 2024 — McMap. All rights reserved.