日本語の文字は、(任意の国際文字になるように)確かに問題です。 URLで許可される文字は非常に限られています。それらが文字列に存在する場合は、URL
イニシャライザはnil
を返します。これらの文字はパーセントエスケープされていなければなりません。
最近では、URLComponents
を使用してそのURLをパーセントエンコードしています。たとえば:マニュアルパーセントエンコーディングスイフト2の答えを
var components = URLComponents(string: "https://www.googleapis.com/youtube/v3/search")!
components.queryItems = [
URLQueryItem(name: "part", value: "snippet,id"),
URLQueryItem(name: "maxResults", value: "50"),
URLQueryItem(name: "order", value: "viewCount"),
URLQueryItem(name: "q", value: "ポケモンGO"),
URLQueryItem(name: "key", value: apikey)
]
components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B") // you need this if your query value might have + character, because URLComponents doesn't encode this like it should
let url = components.url!
、prior revision of this answerを参照してください。
出典
2016-07-14 01:47:36
Rob
有用なコメント:あなたのマテリアルを上回っている人を知っている場合は、相談してください。 – halfer
だからポケモンGOもスタックオーバーフローを克服しました:-) –
あなたはこれを参照することができますhttp://stackoverflow.com/questions/32064754/how-to-use-stringbyaddingpercentencodingwithallowedcharacters-for-a-url-in-swi –