amagain amagain - 29 days ago 12
iOS Question

Cannot call a value of non-function type DateCallBack

The piece of code I am writing used to work charm before migrating to swift 3. But the, doomsday, I am migrating it to XCode 8 and I can't seem to resolve this bug in my code. This issue is out of my understanding. I think I have missed the basics over here.

DateCallBack
is a typealias I defined before declaring my class.

typealias DateCallBack = ((_ year: Int?, _ month: Int?) -> Void)


Then I have declared an instance of DateCallBack.

private var callBack: DateCallBack!


And then within an extension of the class, I have implemented two methods

func cancelButtonAction() {
self.callBack(year: nil, month: nil)
self.animateOut { (completion) in
if completion {
self.removeFromSuperview()
}
}
}

func doneButtonAction(_ yearIndex: Int!, monthIndex: Int!) {
self.callBack(year: yearIndex, month: monthIndex)
self.animateOut { (completion) in
if completion {
self.removeFromSuperview()
}
}
}


And the error I get is
Cannot call a value of non-function type DateCallBack
on both functions self.callBack that is second line of both the functions.
Please explain me what I have missed too.

Answer

You defined the function is defined as (_ year: Int?, _ month: Int?) -> Void so there is no parameter name when it's called (because of the _ before each parameter).

So you call it like this:

self.callBack(yearIndex, monthIndex) 

If you want the function call to look like in your code

self.callBack(year: yearIndex, month: monthIndex

you should define the function like below:

(year: Int?, month: Int?) -> Void
Comments