Ios开发swiftiOS知识点

Swift4.0新特性之String、Array和Diction

2017-08-09  本文已影响566人  TitanCoder

Swift4.0新特性之String、Array和Dictionary


Swift 4是苹果计划在2017年秋季推出的最新版本,值得关注的是其提供了与Swift 3代码很好的兼容性,并最大限度的保持了ABI稳定性.

String

字符串已经像之前的2.0版一样, 改为了Collection类型 。此变化消除了字符串对字符数组的依赖

0.创建空字符串的两种方式,可用isEmpty判断是否为空

let SwiftString = "Hello"
var emptyString = ""               // empty string literal
var anotherEmptyString = String()

1.字符和字符串

1.1 遍历字符串

for char in SwiftString {
    print(char)
}

1.2通过 Character类型创建单个字符

let char : Character = "H"

1.3 String值可以通过传入 Character来构造:

let charArr : [Character] = ["H", "e", "l", "l", "o"]
print(String(char)) // H
print(String(charArr)) // Hello

1.4 拼接字符串

//方式一:
let addString = String(charArr) + String(char) //HelloH
//方式二:
var addString2 = "Why"
addString2 += String(charArr) //"WhyHello"

2.字符串个数

let count1 = SwiftString.count //5(类型: String.CharacterView.IndexDistance)
let count2 = SwiftString.characters.count // 5(Int型)
count1: 

3.是否为空

let isStr = SwiftString.isEmpty //false

4.去掉某一个或几个字符

let text1 = SwiftString.dropFirst()//"ello"
let text2 = SwiftString.dropLast()//"Hell"
let text3 = SwiftString.dropFirst(2)//"llo"
let text4 = SwiftString.dropLast(2)//"Hel"
//注:以上皆为String.SubSequence类型
let text5 = String(text1) //"ello"---text5为String类型

5.倒叙

let text6 = String(SwiftString.reversed()) //"olleH"

6.字符串分割数组

let swiftString2 = "one two three"
let StrArr = swiftString2.split(separator: " ") //["one", "two", "three"]
//let StrArr: [String.SubSequence]

7.删除不符合ASCII编码的字符

let swiftString3 = "quanjun 🐷,"
let qj = swiftString3.filter { (char) -> Bool in
    return char.unicodeScalars.reduce(true, { $0 && $1.isASCII })
}
print(qj)  //"quanjun ,"

8. 字符串索引

8.1 startIndex和endIndex

let greeting = "Guten Tag!"
//startIndex属性来访问 String中第一个 Character的位置
print(greeting[greeting.startIndex])
print(greeting[greeting.index(after: greeting.startIndex)])
//endIndex属性就是 String中最后一个字符后的位置。endIndex属性并不是字符串下标脚本的合法实际参数
//greeting[greeting.endIndex] //会报错
print(greeting[greeting.index(before: greeting.endIndex)])
//注: 如果 String为空,则 startIndex与 endIndex相等

let index = greeting.index(greeting.startIndex, offsetBy: 2)
print(greeting[index])
let indesString = greeting[greeting.startIndex...index]  //"Gut"
//注:indesString : Substring类型
print(String(indesString)) //"Gut"

8.2使用 characters属性的 indices属性来创建所有能够用来访问字符串中独立字符的索引范围 Range。

for index in greeting.characters.indices {
    print("\(greeting[index]) ", terminator: "")//不换行输出
}

9.多行字符串

let rowsString = """
                我是一只小鸭子
                咿呀咿呀哟
                \"""
                """
print(rowsString)

Array

1. 创建确定大小且元素都设定为相同默认值的数组。

/*Array*/
//默认值(叫做 repeating)和新数组元素的数量(叫做 count)
var array1 = Array(repeating: 1, count: 2)
var array2 = Array(repeating: 3, count: 2)

2. 连接两个数组来创建数组(类型必须一致)

var array3 = array1 + array2
print(array3) //[1, 1, 3, 3]

3. 数组个数

print(array3.count)  //5

4. 判断数组个数是否为0

print(array3.isEmpty)  //false
print(array3.count == 0)  //false

5. 添加

array3.append(4) //[1, 1, 3, 3, 4]
array3 += [5]  //[1, 1, 3, 3, 4, 5]
array3 += [6, 5]  //[1, 1, 3, 3, 4, 5, 6, 5]

6. 取值

print(array3[0]) // 1
array3[1] = 9 // [1, 9, 3, 3, 4, 5, 6, 5]

//用下标改变一个范围的值,就算替换与范围长度不同的值的合集也行
array3[2...4] = [0, 8] //[1, 9, 0, 8, 5, 6, 5]

7.遍历

for item in array3 {
    print(item)
}
//元组输出
for (index, item) in array3.enumerated() {
    print("\(index)---\(item)")
}
//倒叙输出
for item in array3.reversed() {
    print(item)
}

//forEach遍历,如同for-in
let arr = [1, 9, 3, 3, 4, 5, 6, 5]
arr.forEach { (x) in
    print(x)
}

8. 删除和插入

array3.remove(at: 1)  //[1, 0, 8, 5, 6, 5]
array3.removeFirst()  //[0, 8, 5, 6, 5]
array3.removeLast()   //[0, 8, 5, 6]
array3.removeFirst(2) //[5,6]
array3.removeLast(1)  //[5]
array3.removeAll()    //[]

Dictionary and Set

1.首先列表可以是从一系列键值对(元组)创建一个字典:

//Dictionary
let keyNames = ["a", "b", "c", "d", "e"]
let valueNames = [1, 2, 4, 5, 6]

//public init<S>(uniqueKeysWithValues keysAndValues: S) where S : Sequence, S.Element == (Key, Value)
//对于该方法的具体解释,可以点进去看一下官方的讲解
let nameDic = Dictionary(uniqueKeysWithValues: zip(keyNames, valueNames))
print(nameDic)
//["b": 2, "e": 6, "a": 1, "d": 5, "c": 4]

2.在初始化Dictionary时, 你现在可以使用你喜欢的方式来处理重复的键,同时避免覆盖键值对,且不会出现任何问题:

let keyNames2 = ["a", "b", "c", "a", "b"]

//public init<S>(_ keysAndValues: S, uniquingKeysWith combine: (Value, Value) throws -> Value) rethrows where S : Sequence, S.Element == (Key, Value)
//对于该方法的具体解释,可以点进去看一下官方的讲解
let nameDic2 = Dictionary(zip(keyNames2, repeatElement(1, count: keyNames2.count)), uniquingKeysWith: +)
print(nameDic2)
//["b": 2, "a": 2, "c": 1]

3.Dictionary 和 Set现在都可以将结果 通过filter函数 过滤到原始类型的新对象中:

let nameDic3 = nameDic.filter({ $0.value < 5 })
print(nameDic3)
//["b": 2, "a": 1, "c": 4]

4.Dictionary为直接映射其值提供了一种非常有用的方法::

//将value值Int转化为字符串
let nameDic4 = nameDic3.mapValues({ "\($0)" })
print(nameDic4)
//["b": "2", "a": "1", "c": "4"]

5.在Dictionary上访问某个值时,常见的做法是使用nil-coalescing operator给出默认值

let nameDic5 = nameDic4["d", default : "unknown"]

6.我们可以从Sequence"中初始化Dictionary,并将其分组为bucket::

//注: 当通过特定模式对数据进行分组时,这相当方便。
let name2 = ["Proxima", "Centauri A", "Centauri B", "Barnard", "Wolf"]
let nameDic6 = Dictionary(grouping: name2, by: { $0.first! })
print(nameDic6)
//["B": ["Barnard"], "W": ["Wolf"], "P": ["Proxima"], "C": ["Centauri A", "Centauri B"]]

涉及Zip的用法,详见Swift语法之Zip详解

7.预留空间

var name3 : [String : Int] = [ : ]
print(name3.capacity) // 0
name3.reserveCapacity(10) //储备 10元素的能力
print(name3.capacity) // 12

上一篇下一篇

猜你喜欢

热点阅读