本文是学习《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中可以利用单个数据单元就可以表示表情符号等数据。