1. 其一是 获得字符数的属性。
在 在我最开始使用 Swift 编程时,我惊奇的发现 Swift的 String 结构没有获得字符串长度的 `size`,`length`,`count` 之类的属性或者方法。 然后发现可以使用 `countElements` 这样一个全局方法。
然后在 Swift 1.2 中这个方法名称简化成 `count` 。
虽然说也有其他编程语言如 Python 是用一个全局的内置函数 `len` 来求字符数 (1)的。
但是作为习惯了 `NSString` 来说,这有一点点难受。 所以一般会在 `String` 的 extension
中加上 `count` 或者 `length` 这么一个属性。
但是从 Swift 2.0 开始 对于 `String` 结构来说,`count` 方法已经不能用了。
经过我的探索,我发现,正确的获得字符串方法。变成了如下方法:
```Swift
let hello = "Hello,world"
hello.characters.count // 11
```
当然如果你误用了,Swift 提供的另一个以 `length` 开头的 `lengthOfBytesUsingEncoding` 方法,就可能有麻烦了。因为 它确实是统计字符所使用字节数的 :
```Swift
let hello = "Hello,world"
hello.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) // 11
let hello2 = "Hello,世界"
hello2.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) // 14
```
2. Swift 中另一个常用操作变得不很不直观的地方就是,substring 类的操作了。
一开始,或者从 `NSString` 时代的操作习惯,获得一个字符串的前三个字符,可能这么写:
```Swift
let hello = "Hello,world"
let hel = hello.substringToIndex(3)
```
或者想着 Swift 字符串可能支持切片,这样写: `hello[0..3]`
但是这些都会报编译错误。
之前我是将其转成 `NSString` 再操作的。 后来在 WWDC 上发现了下面的 Swift Style 的写法:
```Swift
let hello = "Hello,world"
let hel = hello.substringToIndex(advance(hello.startIndex, 3)) // "Hel"
```
核心在于这里的 Index 不是整形的,而是一个 `String.Index` 结构。
`String` 结构本身提供了 `startIndex` 和 `endIndex`
然后想得到其他位置的 Index 你一般就需要使用 这个全局的 `advance` 方法。
当然如果对 `Index` 结构多做些研究,根据它提供的 `successor` 方法,上面任务可以使用如下代码完成:
```Swift
let hello = "Hello,world"
let hel2 = hello.substringToIndex(hello.startIndex.successor().successor().successor()) // "Hel"
```
值得注意的是,为了防止越界,如果是取得最多18个字符,可以这样改写:
```Swift
let hello = "Hello,world"
let hel = hello.substringToIndex(advance(hello.startIndex, 18,hello.endIndex)) // "Hello,world"
```
在 在我最开始使用 Swift 编程时,我惊奇的发现 Swift的 String 结构没有获得字符串长度的 `size`,`length`,`count` 之类的属性或者方法。 然后发现可以使用 `countElements` 这样一个全局方法。
然后在 Swift 1.2 中这个方法名称简化成 `count` 。
虽然说也有其他编程语言如 Python 是用一个全局的内置函数 `len` 来求字符数 (1)的。
但是作为习惯了 `NSString` 来说,这有一点点难受。 所以一般会在 `String` 的 extension
中加上 `count` 或者 `length` 这么一个属性。
但是从 Swift 2.0 开始 对于 `String` 结构来说,`count` 方法已经不能用了。
经过我的探索,我发现,正确的获得字符串方法。变成了如下方法:
```Swift
let hello = "Hello,world"
hello.characters.count // 11
```
当然如果你误用了,Swift 提供的另一个以 `length` 开头的 `lengthOfBytesUsingEncoding` 方法,就可能有麻烦了。因为 它确实是统计字符所使用字节数的 :
```Swift
let hello = "Hello,world"
hello.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) // 11
let hello2 = "Hello,世界"
hello2.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) // 14
```
2. Swift 中另一个常用操作变得不很不直观的地方就是,substring 类的操作了。
一开始,或者从 `NSString` 时代的操作习惯,获得一个字符串的前三个字符,可能这么写:
```Swift
let hello = "Hello,world"
let hel = hello.substringToIndex(3)
```
或者想着 Swift 字符串可能支持切片,这样写: `hello[0..3]`
但是这些都会报编译错误。
之前我是将其转成 `NSString` 再操作的。 后来在 WWDC 上发现了下面的 Swift Style 的写法:
```Swift
let hello = "Hello,world"
let hel = hello.substringToIndex(advance(hello.startIndex, 3)) // "Hel"
```
核心在于这里的 Index 不是整形的,而是一个 `String.Index` 结构。
`String` 结构本身提供了 `startIndex` 和 `endIndex`
然后想得到其他位置的 Index 你一般就需要使用 这个全局的 `advance` 方法。
当然如果对 `Index` 结构多做些研究,根据它提供的 `successor` 方法,上面任务可以使用如下代码完成:
```Swift
let hello = "Hello,world"
let hel2 = hello.substringToIndex(hello.startIndex.successor().successor().successor()) // "Hel"
```
值得注意的是,为了防止越界,如果是取得最多18个字符,可以这样改写:
```Swift
let hello = "Hello,world"
let hel = hello.substringToIndex(advance(hello.startIndex, 18,hello.endIndex)) // "Hello,world"
```