Andrius Steponavičius
Andrius Steponavičius

Reputation: 8184

Swift 2.0, closure = function()

Hey I have a question that I cant seem to get it work

I have closure type

public typealias VoidCompletionHandler = ()->Void

Then I create a optional variable

var favouritePropertyStateHandler:VoidCompletionHandler?

Then I call it

self.favouriteCellStateHandler?()

All works good

self.propertyModel?.favouritePropertyStateHandler = { self.favouriteStateChanged() }

Woks perfectly with fucntion type

func favouriteStateChanged()->Void

But why cant I just

self.propertyModel?.favouritePropertyStateHandler = self.favouriteStateChanged()

Types match - both are ?

()->Void  

The error I get is

Cannot assign a value of type 'Void' ('aka '()') to a value of type 'VoidCopletionHanlder?'

Solved

self.propertyModel?.favouritePropertyStateHandler = self.favouriteStateChanged

However that creates another problem, how do I not cause eternal retain cycle? if I want self to be weak?

Upvotes: 1

Views: 188

Answers (1)

Brad Larson
Brad Larson

Reputation: 170309

You're assigning the result from self.favouriteStateChanged(), not the function itself. Try

self.propertyModel?.favouritePropertyStateHandler = self.favouriteStateChanged

instead.

Upvotes: 3

Related Questions