1

Here is a trivialized example of my problem:

class Test: NSObject {
    func metaHandler(name elementName: String, attributes attr : NSDictionary) -> NSNumber
    {
        return NSNumber.init()
    }
    var metaHandlerFunc: ((String, NSDictionary) -> NSNumber) = metaHandler
}

I get a compiler error when I assign the function to the var:

Cannot convert value of type '(Test) -> (String, NSDictionary) -> NSNumber' to specified type '(String, NSDictionary) -> NSNumber'

My example seems to be exactly what I have seen illustrated elsewhere. Any suggestions?

I'm running Xcode 10.3.

2 Answers 2

1

You have defined metaHandler as an instance method, i.e. as a method which is applied to a concrete instance of Test. As such it has the signature

(Test) -> (String, NSDictionary) -> NSNumber

see for example Instance Methods are “Curried” Functions in Swift.

Defining it as a “type method” (with class or static) solves the problem:

static func metaHandler(...)

Now it has the signature (String, NSDictionary) -> NSNumber and can be assigned to var metaHandlerFunc.

Choose class func if overriding the method in a subclass should be allowed.

Sign up to request clarification or add additional context in comments.

2 Comments

While this is true, the problem (at least when I am running the provided example) reported is that "property initializers run before 'self' is available". So a lazy var seems to solve the problem. Am I missing something?
@Alladinian: You are right, that would solve the compiler error as well (feel free to post as an answer).
0

The problem is that you're trying to assign an instance function to an instance variable, which means that the instance should be initialized before assignment.

Btw the error I get (Swift 5.2.4) is:

cannot use instance member 'metaHandler' within property initializer; property initializers run before 'self' is available

which is a lot more helpful and can be solved by making metaHandlerFunc lazy:

lazy var metaHandlerFunc: ((String, NSDictionary) -> NSNumber) = metaHandler

alternatively, you can make the function static as stated in Martin's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.