本文是學習《The Swift Programming Language》整理的相關隨筆,基本的語法不作介紹,主要介紹Swift中的一些特性或者與OC差異點。
系列文章:
Strings and Characters
字符串的多行處理
If you need a string that spans several lines, use a
multiline string literal. A multiline string literal is a
sequence of characters surrounded by three double quotes
- 需要處理多行的字符串時需要前后加入三個雙引號
例子1:
let multilineString = """hello
world
hello
""";
print("string:\(multilineString)");
執行結果:
string:hello
world
hello
例子2:
let multilineString = """hello
world
hello
""";
print("string:\(multilineString)");
編譯報錯:
Playground execution failed:
error: MyPlayground.playground:105:17: error: multi-line string literal content must begin on a new line
let multilineString = """hello
In its multiline form, the string literal includes all of
the lines between its opening and closing quotes. The
string begins on the first line after the opening quotes
(""") and ends on the line before the closing quotes
("""), which means that quotation doesn’t start or end
with a line feed. Both of the strings below are the same
- 引文并不是從換行開始與結束的
例子3:
let singleLineString = "hello";
let multilineString = """
hello
""";
print("Equal:\(singleLineString == multilineString)");
執行結果:
Equal:true
To make a multiline string literal that begins or ends
with a line feed, write a blank line as the first or last
line. For example
- 如果需要使用到換行符,則必須前后加入空白行
例子4:
let singleLineString = "hello";
let multilineString = """
hello
""";
print("\(multilineString)");
執行結果:
hello
Unicode Scalars
Behind the scenes, Swift’s native String type is built
from Unicode scalar values. A Unicode scalar is a unique
21-bit number for a character or modifier, such as U+0061
for LATIN SMALL LETTER A ("a"), or U+1F425 for
- Swift中的String是由Unicode標量組成,這個Unicode標量是一個21位的字符
我們來直接通過例子看看OC中的NSString
與Swift中的String
的區別。
例子1:(Swift)
let greeting = "Koala ??";
print("length:\(greeting.count)");
for index in greeting.indices{
print("\(index) \(greeting[index])");
}
執行結果:
length:7
Index(_base: Swift.String.UnicodeScalarView.Index(_position: 0), _countUTF16: 1) K
Index(_base: Swift.String.UnicodeScalarView.Index(_position: 1), _countUTF16: 1) o
Index(_base: Swift.String.UnicodeScalarView.Index(_position: 2), _countUTF16: 1) a
Index(_base: Swift.String.UnicodeScalarView.Index(_position: 3), _countUTF16: 1) l
Index(_base: Swift.String.UnicodeScalarView.Index(_position: 4), _countUTF16: 1) a
Index(_base: Swift.String.UnicodeScalarView.Index(_position: 5), _countUTF16: 1)
Index(_base: Swift.String.UnicodeScalarView.Index(_position: 6), _countUTF16: 2) ??
例子2:(OC)
NSString *string = @"Koala ??";
NSLog(@"%ld",string.length);
for(int i = 0; i < string.length;i++){
NSLog(@"%c",[string characterAtIndex:i]);
}
執行結果:
2017-07-11 22:40:17.108159+0800 SwiftTest[2688:127197] length:8
2017-07-11 22:40:17.108332+0800 SwiftTest[2688:127197] K
2017-07-11 22:40:17.108444+0800 SwiftTest[2688:127197] o
2017-07-11 22:40:17.108549+0800 SwiftTest[2688:127197] a
2017-07-11 22:40:17.108753+0800 SwiftTest[2688:127197] l
2017-07-11 22:40:17.108870+0800 SwiftTest[2688:127197] a
2017-07-11 22:40:17.108969+0800 SwiftTest[2688:127197]
2017-07-11 22:40:17.109184+0800 SwiftTest[2688:127197] =
2017-07-11 22:40:17.109285+0800 SwiftTest[2688:127197] (
由上述的例子得出OC中的NSString
與Swift中String
的編碼格式是不一樣的,我們查看NSString
的官方文檔:
An NSString object encodes a Unicode-compliant text
string, represented as a sequence of UTF–16 code units.
這也就說明了同樣的字符串為什么Swift中長度少一位,OC中采用的是16位的數據單元表示數據,而Swift中采用的21位的數據單元表示數據,因此Swift中可以利用單個數據單元就可以表示表情符號等數據。