Categories
Mastering Development

Strange error message when assigning Swift function with arguments to Objective-C property

I would like to set some properties of an Objective-C class instance to some Swift functions and I am getting an error that I do not understand, maybe something with the types is wrong:

Objective-C:

@interface MyWrapper : NSObject
@property (nonatomic) void(*f1)();
@property (nonatomic) void(*f2)(void*);
@end

Swift:

func saySomething1() {
    print("Something 1")
}

func saySomething2(buf:UnsafeRawPointer) {
    print("Something 2")
}

let x = MyWrapper()
x.f1 = saySomething1 // OK
x.f2 = saySomething2 // error: Cannot assign to property: 'x' is a 'let' constant

Changing let to var does not help (same error message).

Leave a Reply

Your email address will not be published. Required fields are marked *