我同时使用 obj-c 和 swift 类。在一个 swift 类(class)中,我尝试将 objective c 代码转换为 swift。但是,我对 NSURL 有疑问。
原代码为:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@://", appItem.URLSchema]];
URLSchema 在头文件中声明如下:
@property (nonatomic, copy) NSString *URLSchema;
我将上面的 objective-c 代码转换为 swift:
var url: NSURL = NSURL(string:"%@://",relativeToURL: appItem.URLSchema)
但是它说“调用中缺少参数“路径”的参数”
当我尝试这样做时:
var url: NSURL = NSURL.URLWithString("%@://", appItem.URLSchema)
它说调用中有额外的参数。
您建议如何正确转换它?
最佳答案
第二个参数:RelativeToURL
的类型为 NSURL
并且您传递一个字符串
试试这个:
var url:NSURL = NSURL(string: "\(appItem.URLSchema)://")
有关更多信息,您可以查看“Swift 编程语言”中的“字符串插值”部分 iBook .
String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. Each item that you insert into the string literal is wrapped in a pair of parentheses, prefixed by a backslash
关于ios - NSURL "missing argument for parameter in call" swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25400643/