Swift 3 : ObjC Class and Protocol same name

Hi,

After a painful migration to Swift 3, i'm facing a last problem.


I have an ObjC Object and an ObjC Protocol that have the same name (and i can't rename it, it's generated code).

This works fine in ObjC, but not in swift "XXX is ambigous for type lookup in this context"


Here is an Example :


// Objc file Operation.h
@interface Operation: NSObject {
    NSString* _name;
}
@end


// Objc file Service.h
@protocol Operation <NSObject>
- (void)doSomething
@end


// Swift File
var operation: Operation! <-This produce an error : 'Operation' is ambiguous for type lookup in this context


Is it possible to specify the type of operation ?


Many thanks

Ben

Answered by OOPer in 189946022

It's not because your class and protocol are having the same name, but because your class has the same name as the one in Foundation.


Operation

Swift 3 imports Objective-C class `NSOperation` with renaming it to `Operation`, so it's confliction with your `Operation`.

And your protocol `Operation` is imported as `OperationProtocol` as well as the protocol `NSObject` is imported as `NSObjectProtocol`.


You can use 3 `Operation`s in your code like this:

var nsOperation: Foundation.Operation!
var myOperation: YourProductModuleName.Operation!
var operationProtocol: OperationProtocol!

Do you import it as frameworks Operation and Service ?


If so, you should try to add the framework name before Operation

No Operation and Service are generated sources file (thrift)

Accepted Answer

It's not because your class and protocol are having the same name, but because your class has the same name as the one in Foundation.


Operation

Swift 3 imports Objective-C class `NSOperation` with renaming it to `Operation`, so it's confliction with your `Operation`.

And your protocol `Operation` is imported as `OperationProtocol` as well as the protocol `NSObject` is imported as `NSObjectProtocol`.


You can use 3 `Operation`s in your code like this:

var nsOperation: Foundation.Operation!
var myOperation: YourProductModuleName.Operation!
var operationProtocol: OperationProtocol!

Ok this is too obvious ! I've try a lot of things...but not this one !!! 😮

One thing strange, when i "cmd+click" on the Operation declaration Xcode show me 2 options, 1st : is my Operation Object, the second is my Operation Protocol, never, the Foundation.Operation... otherwise I would have found (😝)


Many thanks OOPer

We developers, users of the latest Xcode, all know that the current Xocde is far from kind or helpful... You can find the Report Bugs link at the bottom of this page.

Swift 3 : ObjC Class and Protocol same name
 
 
Q