Assign function to struct property

Hi


I'm trying to figure out why the compiler is throwing this error at me:

Cannot convert value of type '((Bool) -> Void) -> ()' to expected argument type '(Bool) -> Void'


Here is my struct and initialization:


struct Item {
  var title = String()
  var didFail = Bool()
  var execute: ((Bool) -> Void)
}

func doSomething(completion: @escaping (Bool) -> Void) {
  completion(true)
}

var item = Item(title: "Fred", didFail: false, execute: doSomething)

item.execute { result in
  print("Result: \(result)")
}

Any ideas as to why this error might be happening. I refactored the execute to take an argument and no closure and it worked fine; but I'd really like to use the callback approach.


Many thanks


Craig

Accepted Answer

Compiler cannot match the different function types.


This works:


struct Item {
    var title = String()
    var didFail = Bool()
    var execute: (@escaping (Bool) -> Void) -> ()
}

func doSomething(completion: @escaping (Bool) -> Void) {
    completion(true)
}

var item = Item(title: "Fred", didFail: false, execute: doSomething)

item.execute { result in
    print("Result: \(result)")
}

Oh perfect, thank you very much!!

Assign function to struct property
 
 
Q