2021-05-07

Swift系列二

一、if-else

  • if后面的条件可以省略小括号
  • 条件后面的大括号不可以省略
let age = 10if age >= 18 { print("大学")} else if age >= 16 { print("高中")} else { print("义务")}
  • if后面的条件只能是Bool类型

二、while

var num = 3while num > 0 { print("num is \(num)") num -= 1}/* 输出: num is 3 num is 2 num is 1 */
  • swift中没有do-while,相同逻辑的是repeat-while
  • swift3开始,去除了自增(++),自减(--)运算符
    • 不要再使用num++num--++num--num等运算符了
    • 自增自减运算符在不同平台有不一样的效果,所以swift放弃了使用
var num = -1repeat { print("num is \(num)")} while num > 0/* 输出:num is -1 */

三、for

3.1. 区间运算符

格式:a...b(a <= 取值 <= b)

  • 变量默认是let修饰,可以自己根据情况修改为var
// 常规用法let words = ["aaa", "bbb", "ccc", "ddd"]for i in words { print(i);}/* 输出: aaa bbb ccc ddd */// 区间用法for i in 1...3 { print(i, words[i])}/* 输出: 1 bbb 2 ccc 3 ddd */// var修饰for var i in 1...3 { i += 5 print(i)}/* 输出: 6 7 8 */// 区间定义常量let range = 1...3for i in range { print(i)}/* 输出: 1 2 3 */// 区间值自定义let a = 1let b = 3for i in a...b { print(i)}/* 输出: 1 2 3 */

3.2. 半开区间运算符

格式:a..<b(a <= 取值 < b)

for i in 1..<3 { print(i)}/* 输出: 1 2 */

3.3. 区间运算符用在数组上

// 此处只是示例用法,实际应用时应注意数组越界let words = ["aaa", "bbb", "ccc", "ddd"]for i in words[0...3] { print(i);}

3.4 单侧区间:让区间朝一个方向尽可能的远

// 常规闭区间let words = ["aaa", "bbb", "ccc", "ddd"]for word in words[0...3] {// print(word);}/* 输出: aaa bbb ccc ddd */// 左边代表开始,右边代表数组最大长度for word in words[2...] { print(word)}/* 输出: ccc ddd */// 左边代表0,右边闭区间值for word in words[...2] { print(word)}/* 输出: aaa bbb ccc */// 左边代表0,右边<2for word in words[..<2] { print(word)}/* 输出: aaa bbb */// 无限制,range左边是无穷小的整数let range = ...5print(range.contains(7)) // falseprint(range.contains(1)) // trueprint(range.contains(-2)) // true

四、区间类型(Range)

  • 范围代表的是两个值的区间,它由上下边界进行定义;
  • 不能对 Range 或者 ClosedRange 进行迭代,但是我们可以检查某个元素是否存在于范围中;
  • Range 和 ClosedRange 既非序列,也不是集合类型。有一部分范围确实是序列是因为 0..<10 的类型其实是一个 CountableRang。CountableRange 和 Range 很相似,只不过它还需要一个附加约束:它的元素类型需要遵守 Strideable 协议 (以整数为步⻓)。Swift 将这类功能更强的范围叫做可数范围,这是因为只有这 类范围可以被迭代。可数范围的边界可以是整数或者指针类型,但不能是浮点数类型,这是由 于 Stride 类型中有一个整数的约束。如果你想要对连续的浮点数值进行迭代的话,你可以通过 使用 stride(from:to:by) 和 stride(from:through:by) 方法来创建序列用以迭代。
--半开闭合
ComparableRangeClosedRange
Strideable(以整数为步长)CountableRangeCountableClosedRange

4.1. 同时省略掉上、下两个边界,这样你将会得到整个集合类型的切片

let arr = [1,2,3,4]arr[...] // [1, 2, 3, 4] type(of: arr) // Array<Int>
// 半开区间let rang: Range = 0.0..<1.0let countableRange: CountableRange = 0..<1// 闭区间let closedRange: ClosedRange = 0.0...1.0 let countableClosedRange: CountableClosedRange = 0...1 // 右侧闭区间let partialRangeThrough: PartialRangeThrough = ...1.0// 左侧闭区间let partialRangeFrom: PartialRangeFrom = 0.0...// 右侧开区间let partialRangeUpTo: PartialRangeUpTo = ..<1.0// 左侧闭区间let countablePartialRangeFrom: CountablePartialRangeFrom = 1...

4.2. 字符、字符串也能使用区间运算符,但默认不能用在for-in中

let stringRange1 = "cc"..."ff"stringRange1.contains("cb") // falsestringRange1.contains("dz") // truestringRange1.contains("e") // truestringRange1.contains("fg") // falselet stringRange2 = "a"..."f"stringRange2.contains("a") // truestringRange2.contains("e") // truestringRange2.contains("g") // false// \0到~囊括了所有可能要用到的ASCII字符let characterRange: ClosedRange<Character> = "\0"..."~"characterRange.contains("G") // true

4.3 带间隔的区间值

let startValue = 0let endValue = 10let interval = 2// value取值从startValue开始,每次间隔interval,不超过endValuefor value in stride(from: startValue, through: endValue, by: interval) { print(value)}/* 输出: 0 2 4 6 8 10 */ // 另外一个方法 to是开区间 public func stride<T>(from start: T, to end: T, by stride: T.Stride) -> StrideTo<T> where T : Strideable

五、switch

5.1. case、default后面不能写大括号(编译器会报错)

5.2. 默认情况下可以不写break,并不会贯穿到后面的条件

enum HeaderCode {case success, failure, redirect, connect}let code = HeaderCode.successswitch code {case .success: print("success")case .redirect: print("redirect")case .connect: print("connect")default: print("failure")}/* 输出:success */

5.3. 如果需要贯穿,怎么办?fallthrough

enum HeaderCode {case success, failure, redirect, connect}let code = HeaderCode.successswitch code {case .success: print("success") fallthroughcase .redirect: print("redirect")case .connect: print("connect")default: print("failure")}/* 输出: success redirect */

5.4. case、default后面至少要有一条语句

5.5. switch必须要保证能处理所有情况

  • 如果不想做任何事,加个break即可
  • 如果能保证已处理所有情况,也可以不使用default
enum HeaderCode {case success, failure, redirect, connect}let code = HeaderCode.successswitch code {case .success: breakcase .redirect: print("redirect")case .connect: print("connect")case .failure: print("failure")}

5.6. 复合条件(switch支持Character、String类型)

// Stringlet string = "idbeny"switch string {case "idbeny": print("idbeny") fallthroughcase "developer": print("developer")default: print("man")}/* 输出: idbeny developer */// Characterlet character: Character = "a"switch character {case "a", "A": print("aaa")default: print("not found")}/* 输出:aaa */

5.7. 区间匹配、元组匹配

// 区间匹配let count = 10switch count {case 0: print("zero")case 1...5: print("few")case 10...100: print("large")default: print("not found")}/* 输出:large */// 元组匹配let point = (1, 1)switch point {case (0, 0): print("origin")case (_, 0): print("x-axis")case (0, _): print("y-axis")case (-2...2, -2...2): print("in the box")default: print("not found")}/* 输出:in the box */
  • 可以用下划线(_)忽略某个值
  • 值绑定(必要时let可以改为var)
let point1 = (2, 0)switch point1 {case (let x, 0): print("on the x-axis and x value of \(x)")case (0, let y): print("on the y-axis and y value of \(y)")case (let x, let y): print("at (\(x), \(y))")}/* 输出:on the x-axis and x value of 2 */

六、where(过滤)

let point = (1, -1)switch point {case let(x, y) where x == y: print("x == y")case let(x, y) where x == -y: print("x == -y")case let(x, y): print("at (\(x), \(y))")}/* 输出:x == -y */// 把所有数加起来var numbers = [10, 20, -30, -40, 50]var sum = 0for num in numbers where num > 0 { sum += num}print(sum)/* 输出:80 */

七、标签语句

side: for i in 1...4 { for k in 1...4 {  if k == 3 {   continue side  }  if i == 3 {   break side  }  print("i == \(i), k == \(k)") }}/* 输出: i == 1, k == 1 i == 1, k == 2 i == 2, k == 1 i == 2, k == 2 */









原文转载:http://www.shaoqun.com/a/726764.html

跨境电商:https://www.ikjzd.com/

瀚霖:https://www.ikjzd.com/w/2345

美森:https://www.ikjzd.com/w/1693


一、if-elseif后面的条件可以省略小括号条件后面的大括号不可以省略letage=10ifage>=18{print("大学")}elseifage>=16{print("高中")}else{print("义务")}if后面的条件只能是Bool类型二、whilevarnum=3whilenum>0{print(&qu
美菜网:https://www.ikjzd.com/w/1874
美森:https://www.ikjzd.com/w/1693
贝贝母婴网:https://www.ikjzd.com/w/1321
亚马逊选品必看!做亚马逊如何选出爆款产品?(上):https://www.ikjzd.com/home/99400
Wish Tags:https://www.ikjzd.com/w/807
口述:老公为了不生孩子夜夜加班:http://lady.shaoqun.com/a/272196.html

No comments:

Post a Comment