Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 588
Patrick.Bellot Avatar asked Oct 05 '15 15:10

Patrick.Bellot


2 Answers

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

like image 188
Eric Aya Avatar answered Nov 02 '22 17:11

Eric Aya


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.

like image 5
Millie H. Avatar answered Nov 02 '22 17:11

Millie H.