2017-04-25 18 views
2

我翻譯的Objective-C代碼斯威夫特3,當我寫這篇文章的代碼斯威夫特URL resourceSpecifier不可用

let url = URL(string:"http://example.com/index.html") 
    var formattedURL:String = url!.resourceSpecifier 

我得到一個錯誤「‘resourceSpecifier’不可用:使用pathqueryfragment相反「。我該如何解決這個問題?

+0

'resourceSpecifier'是NSURL'的'屬性。雖然通常你在Swift 3中使用'URL',你仍然可以使用'NSURL'。 – rmaddy

+0

什麼是URL中resourceSpecifier的等價物? –

+0

沒有一個,這是問題。你可以剝離計劃和冒號來獲得相同的結果。 – rmaddy

回答

3

您可以彌合URLNSURL

let url = URL(string:"http://example.com/index.html")! 
let formattedURL = (url as NSURL).resourceSpecifier 
+0

或者只是使用'NSURL'開頭,如果這是它的唯一用途。 – rmaddy

+0

什麼是URL中的resourceSpecifier相當於我的主要興趣。我知道我可以隨時使用NSURL。 –

+0

'resourceSpecifier'沒有在'URL'中實現。你可以考慮編譯器的建議。 – vadian