相关疑难解决方法(0)

Objective-C/Cocoa Touch中的HTML字符解码

首先,我发现了这个: Objective C HTML escape/unescape,但它对我不起作用.

我编码的字符(来自RSS提要,顺便说一句)看起来像这样: &

我在网上搜索并找到了相关的讨论,但没有修复我的特定编码,我认为它们被称为十六进制字符.

html iphone cocoa cocoa-touch objective-c

102
推荐指数
6
解决办法
9万
查看次数

如何解码 NSString 中的 html 编码字符?

我正在编写一个支持英语和法语的应用程序。应用程序从服务器请求信息,服务器响应使用 JSON。我正在使用 JSONKit 库来解析响应,但从法语响应中解析的字符串如下所示:

Membres –Économisez 5% sur les services et jusqu’à 15% sur d’autres produits

如何解码特殊字符?这样我就得到了这个:

会员 – 服务费用 5%,产品费用 15%

我查看了 NSString 的 API 并尝试了一些实例方法,但我对字符编码了解不多,最终得到了一些奇怪的结果。因此,如果您还可以提供有关字符编码的简要说明,我将非常感激。

提前致谢

iphone character-encoding nsstring ios

4
推荐指数
1
解决办法
1万
查看次数

SwiftUI 解码 HTML 实体错误:同时访问 0x7ff43ff29b50,但修改需要独占访问

当我使用 解码 html entites 时出现错误NSAttributedString。我使用 Swift 5。我只想将 html 字符串化。我不想要网络视图。

SwiftUI 文本

Text("It&#39s a party!".decoded)
Run Code Online (Sandbox Code Playgroud)

字符串扩展

extension String {
    var decoded: String {
        let attr = try? NSAttributedString(data: Data(utf8), options: [
        .documentType: NSAttributedString.DocumentType.html,
        .characterEncoding: String.Encoding.utf8.rawValue
    ], documentAttributes: nil)

        return attr?.string ?? self
    }
}
Run Code Online (Sandbox Code Playgroud)

错误日志

== AttributeGraph: cycle detected through attribute 123464 ===
=== AttributeGraph: cycle detected through attribute 143224 ===
=== AttributeGraph: cycle detected through attribute 128744 ===
Simultaneous accesses to 0x7ff43ff29b50, but modification requires exclusive access.
Previous …
Run Code Online (Sandbox Code Playgroud)

decode swiftui

4
推荐指数
1
解决办法
1080
查看次数

Swift HTML 解码问题

在 Swift 中,我使用 Decoding HTML NSAttributedString,如下所示:

\n\n
let encodedString = "Ph\xe1\xba\xa3i c\xc3\xb4ng nh\xe1\xba\xadn r\xe1\xba\xb1ng k\xe1\xbb\x83 t\xe1\xbb\xab l\xc3\xbac \xc3\xb4ng Th\xc4\x83ng\xc2\xa0l\xc3\xa0m b\xe1\xbb\x99 tr\xc6\xb0\xe1\xbb\x9fng"\nlet encodedData = encodedString.dataUsingEncoding(NSUTF8StringEncoding)\nlet attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]\nlet attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)         \nlet decodedString = attributedString.string\nprintln(decodedString)\n
Run Code Online (Sandbox Code Playgroud)\n\n

但结果是这样的:

\n\n
\n

Ph\xc3\xa1\xc2\xba\xc2\xa3i c\xc3\x83\xc2\xb4ng nh\xc3\xa1\xc2\xba\xc2\xadn r\xc3\xa1\xc2\xba\xc2\xb1ng k\ xc3\xa1\xc2\xbb\xc6\x92 t\xc3\xa1\xc2\xbb\xc2\xab l\xc3\x83\xc2\xbac \xc3\x83\xc2\xb4ng Th\xc3\x84\xc6\x92ng \xc3\x82\xc2\xa0l\xc3\x83\xc2\xa0m b\xc3\xa1\xc2\xbb\xe2\x84\xa2\n tr\xc3\x86\xc2\xb0\xc3\xa1\xc2\xbb \xc5\xb8ng

\n
\n\n

真实结果一定与encodedString

\n\n

这个方法有什么问题吗?

\n

html ios swift

3
推荐指数
1
解决办法
2389
查看次数

在Swift中,您如何转换“°” 到“°”(即度数符号)?使用内置的Cocoa实用程序(带有NSAttributedString)

当我从中读取数据时,使用以下代码说出http://www.weather-forecast.com/

let urlContent = NSString(data: data!, encoding: NSUTF8StringEncoding)
Run Code Online (Sandbox Code Playgroud)

urlContent数据显示为"°"。您如何将其转换为“°”(即度数符号)?

nsdata ios swift

3
推荐指数
2
解决办法
5332
查看次数

如何在swift 2.0中解码HTML实体?

我在Stackoverflow上找到了这个扩展.但是,出现了错误.我该如何解决这个错误?

Cannot invoke initializer for type 'NSAttributedString' with an argument list of type '(data: NSData, options: [String : AnyObject], documentAttributes: NilLiteralConvertible, error: NilLiteralConvertible)'
Run Code Online (Sandbox Code Playgroud)

错误在"let attributionString"中.

extension String {
    init(htmlEncodedString: String) {
        let encodedData = htmlEncodedString.dataUsingEncoding(NSUTF8StringEncoding)!
        let attributedOptions : [String: AnyObject] = [
            NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
            NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
        ]
        let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)! //ERROR HERE!
        self.init(attributedString.string)
    }
}
Run Code Online (Sandbox Code Playgroud)

html swift

0
推荐指数
1
解决办法
2255
查看次数

从API转换Swift中String中的任何UTF-8编码字符

我正在制作一个应用程序,用于检查我学校的成绩和作业.在查看作业时从网上看到: 来自学校网页的图像

但是服务器实际上返回一个包含常规字符的字符串,而中文字符保持常规的UTF-8编码格式: 应用程序中显示的原始字符串

我将如何解析Swift中的原始String并解码任何UTF-8编码的字符..我很难找到甚至在网上找到解决方案.只是一个FYI,我无法改变后端的任何内容.

string utf-8 nsstring ios swift

0
推荐指数
1
解决办法
8181
查看次数