ios - NSURL "missing argument for parameter in call" swift

标签 ios objective-c swift nsurl

我同时使用 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/

相关文章:

ios - 如何使用 Swift 对字典数据进行分组

ios - 从图像路径加载图像时出现问题 iOS Swift

ios - 在表格单元格中隐藏选择元素 - Swift

ios - NSURLConnection sendSynchronousReques 替换

ios - swift中的嵌套递归函数

iphone - iPhone 5 View 布局协助

iphone - UIColor 清除颜色

objective-c - 获取当前键盘光标位置

ios - 如何在 UITableView Cell 中实现类似 iOS Mail 的从左向右滑动

swift - 获取 SecKey 的字符串表示形式