发布于 2015-12-28 10:34:04 | 132 次阅读 | 评论: 0 | 来源: 网友投递
Swift编程语言
SWIFT,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C*共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序。
(1)字符值引用 (numeric character reference, NCR) 是在标记语言SGML以及派生的如HTML与XML中常见的一种转义序列结构,用来表示Unicode的通用字符集 (UCS)中的单个字符. NCR可以表示在一个特定文档中不能直接编码的字符,而该标记语言阅读器软件把每个NCR当作一个字符来处理。
(2)我们可以将其理解为HTML、XML 等 SGML 类语言的转义序列(escape sequence)。而不是一种编码或转码。
1
2
|
中国 //中国(16进制格式) 中国 //中国(10进制格式) |
1
2
3
4
5
6
7
8
9
10
11
12
|
extension String { //转译成字符值引用(NCR) func toHtmlEncodedString() -> String { var result: String = "" ; for scalar in self .utf16 { //将十进制转成十六进制,不足4位前面补0 let tem = String ().stringByAppendingFormat( "%04x" ,scalar) result += "&#x\(tem);" ; } return result } } |
1
2
3
|
let words = "欢迎来到 hangge.com" print (words.toHtmlEncodedString()) //欢迎来到 hangge.com |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
extension String { init (htmlEncodedString: String ) { do { let encodedData = htmlEncodedString.dataUsingEncoding( NSUTF8StringEncoding )! let attributedOptions : [ String : AnyObject ] = [ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType , NSCharacterEncodingDocumentAttribute : NSUTF8StringEncoding ] let attributedString = try NSAttributedString (data: encodedData, options: attributedOptions, documentAttributes: nil ) self . init (attributedString.string) } catch { fatalError( "Unhandled error: \(error)" ) } } } |
1
2
3
|
let words = String (htmlEncodedString: "欢迎来到 hangge.com" ) print (words) //欢迎来到 hangge.com |