带有URL内容的字符串? [英] String With Contents Of URL?

查看:181
本文介绍了带有URL内容的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我下载最新更新之前,以下代码对我有用:

So before I downloaded the recent update, the following code worked for me:

var g_home_url = String.stringWithContentsOfURL(NSURL(string: url_string), encoding: NSUTF8StringEncoding, error: nil) // Gives me an error: "String.Type does not have a member names stringWithContentsOfUrl"

我很困惑。在swift中采用以下Objective-c方法的正确方法是什么?

I am confused. What is the proper way to acieve the following objective-c method in swift?

NSString * g_home_url = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:home_url] encoding:NSUTF8StringEncoding error:nil];


推荐答案

使用 -initWithContentsOfURL: encoding:实例方法而不是 + stringWithContentsOfURL:encoding:error: class convenience initializer。

Use the -initWithContentsOfURL:encoding:error: instance method instead of the +stringWithContentsOfURL:encoding:error: class convenience initializer.

var g_home_url = String(contentsOfURL: NSURL(string: url_string)!, encoding: NSUTF8StringEncoding, error: nil)

我不知道Swift现在是否支持类方便初始化器,但是它们才有意义,因为它们只是alloc-init样板的缩写,它没有'存在于Swift中。

I have no idea if class convenience initializers are now unsupported in Swift, but it would make sense as they were just shorthands for the alloc-init boilerplate, which doesn't exist in Swift.

这篇关于带有URL内容的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆