[英]Swift and Wordpress API: Wordpress API escapes some characters to unicode
我正在復制粘貼Wordpress API返回的2個帖子標題:
Haydarpaşa’da ortaya çıktı! Tam 1700 yıllık…
Pakistan’da terör saldırısı
我為類別/帖子和其他內容創建結構,並使它們可解碼,但這些結構不處理Unicode。 這是一個例子。 我為類別創建的結構。 (帖子的結構太大,因此我共享類別結構。它們都基於相同的思想。)
struct WPCategory: Decodable {
let id: Int
let count: Int
let description: String
let link: URL
let name: String
let slug: String
let taxonomy: WPCategoryTaxonomy
let parent: Int
enum WPCategoryTaxonomy: String, Codable {
case category, postTag = "post_tag", navMenu = "nav_menu", linkCategory = "link_category", postFormat = "post_format"
}
enum CodingKeys: String, CodingKey {
case id, count, description, link, name, slug, taxonomy, parent, meta
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(Int.self, forKey: .id)
count = try container.decode(Int.self, forKey: .count)
description = try container.decode(String.self, forKey: .description)
let linkString = try container.decode(String.self, forKey: .link)
guard let link = URL.init(string: linkString) else {
throw WPAPIError.urlToStringFailed
}
self.link = link
name = try container.decode(String.self, forKey: .name)
slug = try container.decode(String.self, forKey: .slug)
taxonomy = try container.decode(WPCategoryTaxonomy.self, forKey: .taxonomy)
parent = try container.decode(Int.self, forKey: .parent)
}
}
我正在使用Alamofire來獲取數據:
func getCategories(page: Int = 1, onCompletion completionHandler: @escaping (_ categories: [WPCategory]?, _ totalPages: Int?, _ error: Error?) -> Void) {
let request = alamofire.request(categoriesURL, method: .get, parameters: ["page": page, "per_page": 100, "exclude":"117"], encoding: URLEncoding.httpBody).validate()
request.responseData { (response) in
switch response.result {
case .success(let result):
guard let total = response.response?.allHeaderFields["x-wp-totalpages"] as? String else {
completionHandler(nil, nil, WPAPIError.couldNotFetchTotalHeader)
return
}
do {
let categories = try JSONDecoder.init().decode([WPCategory].self, from: result)
completionHandler(categories, Int(total), nil)
} catch(let err) {
completionHandler(nil, nil, err)
}
case .failure(let error):
completionHandler(nil, nil, error)
}
}
}
那么,如何處理這些Unicode字符? 有任何想法嗎? 謝謝。
使用我為此編寫的擴展名:
extension String {
func htmlDocument() throws -> String {
let data = self.data(using: .unicode)
let options: [NSAttributedString.DocumentReadingOptionKey: NSAttributedString.DocumentType] = [.documentType : .html]
return try NSAttributedString(data: data!, options: options, documentAttributes: nil).string
}
}
因此,您可以在解碼器中使用它,例如:
...
name = try container.decode(String.self, forKey: .name).htmlDocument()
...
根據@OOper建議,最好在使用unicode的情況下更新標題和文本。 swift字符串基於unicode,請參考下面的文檔鏈接,該文檔證明了swift標准庫或Apple的框架正確處理了unicode。 因此您提到的標題沒有意義… ’
… ’
這些是數字字符引用,而不是unicode char。
https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html
如果您需要在Swift中處理這樣的轉義字符串,可以將其轉換為普通的 Unicode字符串。
因此,如果在Swift游樂場中輸入以下代碼:
import Foundation
func convert(escapedString: String) -> String {
guard let regex = try? NSRegularExpression(pattern: "(&#([0-9]+);)",
options: []) else { return escapedString }
let escapedNSString = escapedString as NSString
let matches: [NSTextCheckingResult] = regex.matches(in: escapedString,
options: [],
range: NSMakeRange(0, escapedString.count))
var convertedString = escapedNSString
for match in matches.reversed() {
let matchString = escapedNSString.substring(with: match.range(at: 2))
var replacement: String
if let unicode = UnicodeScalar(Int(matchString)!) {
replacement = String(unicode)
} else {
replacement = "?"
}
convertedString = convertedString.replacingCharacters(in: match.range, with: replacement) as NSString
}
return String(convertedString)
}
let str1 = "Haydarpaşa’da ortaya çıktı! Tam 1700 yıllık…"
print (convert(escapedString: str1))
let str2 = "Pakistan’da terör saldırısı"
print (convert(escapedString: str2))
這樣您將得到結果:
Haydarpaşa’da ortaya çıktı! Tam 1700 yıllık…
Pakistan’da terör saldırısı
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.