mardi 4 août 2015

Swift: 'Int' is not convertible to 'ObjCBool'

In my code I have a lazy var:

lazy var myService = {
    return JSObjection.defaultInjector().getObject(MyService) as? MyService
}()

In the same file I try to call a function of myService:

func foo() {
    myService?.getSomeStringsFrom(nil)
}

MyService is defined in an Objective-C source file:

@interface MyService : NSObject
- (NSArray *) getSomeStringsFrom:(NSString *)aString;
@end

@implementation MyService
- (NSArray *) getSomeStringsFrom:(NSString *)aString {
    return nil;
}
@end

The code cannot be compiled because the call of getSomeStringsFrom(nil) triggers an error message (twice):

<unknown>:0: error: 'Int' is not convertible to 'ObjCBool'

This does not make sense - I am doing the same with other services and XCode does not complain about it. Is it a compiler bug? I am using XCode 6.4 and Swift 1.2.

EDIT: After more digging in the code I found out that the error message can also be triggered by instantiating MyService. In the following answer, you will see the real "root" of the problem.

Aucun commentaire:

Enregistrer un commentaire