objective-c - 字符串!不符合协议(protocol) 'ExpressibleByStringLiteral'

标签 objective-c swift3

我正在尝试使用来自 swift 文件的定义为 Objective C 方法的方法

objective-c 中的方法定义为

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

无法重构方法名。

我正在尝试按如下方式调用它

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

作为返回,我收到编译错误:“String! does not conform to protocol 'ExpressibleByStringLiteral'

我该如何解决?

编辑:

MyObject定义如下

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

@implementation

@end`

最佳答案

在 Swift 3 中,ObjC 泛型被导入为 Swift 泛型。而且你总是需要在 Swift 泛型中指定类型参数。 (而且 Swift 3 还没有完善到可以生成适当的诊断...)

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

成功了。

关于objective-c - 字符串!不符合协议(protocol) 'ExpressibleByStringLiteral',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39904343/

相关文章:

objective-c - Xcode:Interface Builder XIB 编译器错误

ios - 将 UIImage 大小与 pdf 大小匹配

ios - 如何在 iOS Swift 中的 GoogleMaps 中绘制 CurrentLocation(locationStart) 到 SearchedLocation(locationEnd) 之间的路线

ios - 在 tableView 中添加填充功能 (Swift)

Swift 3 - 将 segue 推送到导航 Controller

objective-c - swift 私有(private)模块映射,尝试从框架内在 Swift 中使用 objC 代码

iphone - 如何在iPhone App中实现PSCollectionView?

iphone - 将两个“for”语句合并为一个

iphone - 新版本App内购

ios - 如何在不使用 segue 的情况下在 View Controller 之间传递图像