Lena Bru Lena Bru - 1 year ago 170
Objective-C Question

String! does not conform to protocol 'ExpressibleByStringLiteral'

I am trying to use a method that was defined as Objective C method, from a swift file

the method in objective c is defined as

-(instancetype) init:(NSString*)string data:(id)data;

the method name cannot be refactored.

I am trying to invoke it as follows

let myObject = MyObject("MyString",data:["1","2","3"])

in return i am getting the compile error : "
String! does not conform to protocol 'ExpressibleByStringLiteral'

How can I fix it?


MyObject is defined as follows

`@interface MyObject<__covariant Type> : NSObject @end



Answer Source

In Swift 3, ObjC generics are imported as Swift generics. And you always need to specify the type parameter in Swift generics. (And Swift 3 is not yet so refined as to generate appropriate diagnostics...)

let myObject = MyObject<NSString>("MyString",data:["1","2","3"])


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download