1
votes

I'm trying to convert code from Objective-C to Swift. It won't be possible to convert all of it, since the project uses C++ and thus also Objective-C++ but at least the used protocols should be written in Swift.

So while trying to convert one with a block as a property I've stumbled over problems.

My Objective-C protocol:

@protocol ObjcProtocol<NSObject>

@property (nullable, nonatomic, copy, readwrite) dispatch_block_t callback;

@end

My Swift protocol:

@objc public protocol SwiftProtocol: NSObjectProtocol {
    var callback: ()->() { get set }
}

Trying to set this property in Objective-C I get this error:

"Property 'callback' not found on object of type 'NSObject *'"

The created Swift header file contains this:

@property (nonatomic, copy) void (^ _Nonnull callback)(void);

I've also stumbled upon @convention(block). Declaring it directly in the var doesn't work, as I get an error claiming it can only be used as a function type. So I've tried using a typealias:

public typealias callBackType = (@convention(block) () -> ())

which once again results in the property not being found.

Something else I've tried but had no success, was explicitly annotating the property with @objc.

Setting the callback function type to optional also didn't change anything.

Furthermore I've tried creating an Objective-C protocol containing the callback and make the SwiftProtocol depend on the newly created protocol. Same result...

What am I missing?

1

1 Answers

1
votes

Okay.. so it turned out even though Xcode can auto-complete the code, it doesn't mean, everything is correctly imported in the file. To fix the above I've added @import <ModuleName.Swift>; and it works