二十、字面量

本文深入探讨Swift编程中的字面量使用,包括字面量的类型、字面量协议以及如何自定义字面量协议。同时,详细阐述模式匹配的概念,涵盖通配符模式、标识符模式、值绑定模式等多个模式,并通过实例展示了如何在枚举、结构体中应用模式匹配。此外,还介绍了如何在条件语句中使用自定义表达式模式和where子句,帮助开发者更好地理解和运用Swift的模式匹配机制。
摘要由CSDN通过智能技术生成

//1.字面量,10,false,"Jack"都是字面量
//swift自带的绝大部分类型,都支持直接通过字面量进行初始化:Bool,Int,Float,Double,String,Array,Dictionary,Set,Optional等
var age = 10
var isRed = false
var name = "Jack"
//常见字面量的默认类型
//public typealias IntegerLiteralType = Int
//public typealias FloatLiteralType = Double
//public typealias BooleanLiteralType = Bool
//public typealias StringLiteralType = String
//可以通过typealias修改字面量的默认类型,只需知道就行一般不这么做
typealias FloatLiteralType = Float
typealias IntegerLiteralType = UInt8
var age0 = 10
var height0 = 1.68

//2.字面量协议
/*
 swift自带类型之所以能够通过字面量初始化,是因为它们遵守了对应的协议
 Bool:ExpressibleByBooleanLiteral
 Int:ExpressibleByIntegerLiteral
 Float、Double:ExpressibleByIntegerLiteral、ExpressibleByFloatLiteral
 Dictionary:ExpressibleByDictionaryLiteral
 String:ExpressibleByStringLiteral
 Array、Set:ExpressibleByArrayLiteral
 Optional:ExpressibleByNilLiteral
 */
var b: Bool = false //ExpressibleByBooleanLiteral
var i: Int = 10 //ExpressibleByIntegerLiteral
var f0: Float = 10 //ExpressibleByIntegerLiteral
var f1: Double = 10.0 //ExpressibleByBooleanLiteral
var d0: Float = 10 //ExpressibleByIntegerLiteral
var d1: Double = 10.0 //ExpressibleByFloatLiteral
var s: String = "Jack" //ExpressibleByStringLiteral
var arr: Array = [1, 2, 3] //ExpressibleByArrayLiteral
var set: Set = [1, 2, 3] //ExpressibleByArrayLiteral
var dict: Dictionary = ["Jack" : 60] //ExpressibleByDictionaryLiteral
var o: Optional<Int> = nil //ExpressibleByNilLiteral

//3.字面量协议应用:Person对象能不像Int一样用字面量那样初始化呢?可以遵循字面量协议即可
extension Int : ExpressibleByBooleanLiteral {
    public init(booleanLiteral value: Bool) {
        self = value ? 1 : 0
    }
}
var num: Int = true
print(num) //1

class Student : ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, ExpressibleByStringLiteral,
                CustomStringConvertible {
    var name: String = ""
    var score: Double = 0
    required init(floatLiteral value: Double) {
        self.score = value
    }
    required init(integerLiteral value: Int) {
        self.score = Double(value)
    }
    required init(stringLiteral value: String) { //ExpressibleByStringLiteral遵循了3个协议
        self.name = value
    }
    required init(unicodeScalarLiteral value: String) { //特殊字符🐂
        self.name = value
    }
    required init(extendedGraphemeClusterLiteral value: String) {
        self.name = value
    }
    var description: String { "name=\(name),score=\(score)" }
}
var stu: Student = 90
print(stu) //name=,score=90.0
stu = 98.5
print(stu) //name=,score=98.5
stu = "Jack"
print(stu) //name=Jack,score=0.0

//4.字面量协议应用
struct Point {
    var x = 0.0, y = 0.0
}
extension Point : ExpressibleByArrayLiteral, ExpressibleByDictionaryLiteral {
    init(arrayLiteral elements: Float...) { //...表示不确定个数
        guard elements.count > 0 else { return }
        self.x = elements[0]
        guard elements.count > 1 else { return }
        self.y = elements[1]
    }
    init(dictionaryLiteral elements: (String, Float)...) {
        for (k, v) in elements {
            if k == "x" { self.x = v }
            else if k == "y" { self.y = v }
        }
    }
}
var p: Point = [10.5, 20.5]
print(p)//Point(x: 10.5, y: 20.5)
p = ["x" : 11, "y" : 22]
print(p) //Point(x: 11.0, y: 22.0)

//十八、模式匹配
/*
 1.模式
 什么是模式?模式是用于匹配的规则,比如switch的case、捕捉错误的catch、if/guard/while/for语句的条件等
 swift中金的模式有8个:
 通配符模式、标识符模式、值绑定模式、元组模式、
 枚举case模式、可选模式、类型转换模式、表达式模式
 2.通配符模式
 _ 匹配任何值 _?匹配非nil值
 */
enum Life {
    case human(name: String, age: Int?)
    case animal(name: String, age: Int?)
}
func check(_ life: Life) {
    switch life {
    case .human(let name, _):
        print("human", name)
    case .animal(let name, _?):
        print("animal", name)
    default:
        print("other") //animal age=nil空的情况
    }
}
check(.human(name: "Rose", age: 20)) //human Rose
check(.human(name: "Jack", age: nil)) //human Jack
check(.human(name: "Dog", age: 5)) //human Dog
check(.human(name: "Cat", age: nil)) //human Cat

//3.标识符模式:给对应的变量、常量名赋值
var age = 10
var name = "Jack"

//4.值绑定模式
let point = (3, 2)
switch point {
case let (x, y):
    print("The point is at (\(x), \(y)).") //The point is at (3, 2).
}

//5.元组模式
let points = [(0, 0), (1, 0), (2, 0)]
for (x, _) in points {
    print(x)
}
let names: String? = "Jack"
let ages = 18
let infos: Any = [1, 2]

switch (names, ages, infos) {
case (_?, _, _ as String):
    print("case")
default:
    print("default") //default
}

var scores = ["Jack" : 98, "Rose" : 100, "Kate" : 86]
for (name, score) in scores {
    print(name, score)
}

//6.枚举case模式
//if case 语句等价于只有1个case的switch语句
let num = 2
//原来写法
if num >= 0 && num <= 9 {
    print("[0, 9]") //[0, 9]
}
//枚举case模式
if case 0...9 = num{
    print("[0, 9]") //[0, 9]
}
//1个case的switch语句
switch num {
case 0...9:
    print("[0, 9]") //[0, 9]
default:
    break
}

let nums: [Int?] = [2, 3, nil, 5]
for case nil in nums {
    print("有nil值") //有nil值
    break
}

let pointss = [(1, 0), (2, 1), (3, 0)]
for case let (x, 0) in pointss {
    print(x) //1 //3
}

//7.可选模式
let height: Int? = 42
if case .some(let x) = height { print(x) } //等价下一句 //42
if case let x? = height { print(x) } //42

let heights: [Int?] = [nil, 2, 3, nil, 5]
for case let height? in heights {
    print(height) //2 //3 //5
}
//等价下边for
for item in heights {
    if let height = item {
        print(height)
    }
}

func checks(_ num: Int?) {
    switch num {
    case 2?: //非空里边包装是2
        print("2")
    case 4?:
        print("4")
    case 6?:
        print("6")
    case _?:
        print("other")
    case _:
        print("nil")
    }
}
checks(4) //4
checks(8) //other
checks(nil) //nil

//8.类型转换模式
let number: Any = 6
switch number {
//case is Int:  //编译器依然认为number 是Any类型
//    print("is Int", number)
case let n as Int:
    print("is Int", n + 1) //is Int 7
default:
    break
}

class Animal {
    func eat(){
        print(type(of: self), "eat")
    }
}
class Dog: Animal {
    func run() {
        print((type(of: self), "run"))
    }
}
class Cat: Animal {
    func jump() {
        print(type(of: self), "jump")
    }
}
func check(_ animal: Animal) {
    switch animal {
    case let dog as Dog:
        dog.eat()
        dog.run()
    case is Cat:
        animal.eat() //run jump
        (animal as? Cat)?.jump() //Cat jump //不能直接调得转
    default:
        break
    }
}
check(Dog()) //Dog eat //(__lldb_expr_41.Dog, "run")
check(Cat()) //Cat eat

//9.表达式模式:用在case中
let dot = (1, 2)
switch dot {
case (0, 0):
    print("(0, 0) is at origin.")
case (-2...2, -2...2):
    print("(\(dot.0),\(dot.1)) is near the origin.") //(1,2) is near the origin.
default:
    print("The dot is at (\(dot.0),\(dot.1)).")
}

//10.自定义表达式模式
struct Student {
    var score = 0, name = ""
    //pattern:case后面的内容
    //value:switch后面的内容
    static func ~= (pattern: Int, value: Student) -> Bool {
        value.score >= pattern
    }
    static func ~= (pattern: ClosedRange<Int>, value: Student) -> Bool{
        pattern.contains(value.score)
    }
    static func ~= (pattern: Range<Int>, value: Student) -> Bool {
        pattern.contains(value.score)
    }
}
var stu = Student(score: 75, name: "Jack")
switch stu {
case 100: print(">=100")
case 90: print(">=90")
case 80..<90: print("[80, 90)")
case 60..<79: print("[60, 79)")
case 0: print(">=0")
default: break
} //[60, 79)

if case 60 = stu {
    print(">=60") //>=60
}
var info = (Student(score: 70, name: "Jack"), "及格")
switch info {
case let (60, text):
    print(text)
default:
    break
} //及格

//11.自定义表达式模式
extension String {
    static func ~= (pattern: (String) -> Bool, value: String) -> Bool {
        pattern(value)
    }
}
//prefix == "21"
//str == "123455"
func hasPrefix(_ prefix: String) -> ((String) -> Bool) {
    return {
        (str: String) -> Bool in
        str.hasPrefix(prefix)
    }
}
//省略后是 结合省略前去理解下面
//func hasPrefix(_ prefix: String) -> ((String) -> Bool) {
//    {
//        $0.hasPrefix(prefix)
//    }
//}
func hasSuffix(_ suffix: String) -> ((String) -> Bool) {
    {
        $0.hasSuffix(suffix)
    }
}
var str = "jack"
switch str {
case hasPrefix("j"), hasSuffix("k"):
    print("以j开头,以k结尾") //以j开头,以k结尾
default:
    break
}
//不自定义表达式匹配的话等价如下
//if str.hasPrefix("123") || str.hasSuffix("456") {
//
//}

//12.自定义表达式模式
func isEven(_ i: Int) -> Bool {
    i % 2 == 0
}
func isOdd(_ i: Int) -> Bool {
    i % 2 != 0
}
extension Int {
    static func ~= (pattern: (Int) -> Bool, value: Int) -> Bool {
        pattern(value)
    }
}
var numb = 9
switch numb {
case isEven:
    print("偶数")
case isOdd:
    print("奇数") //奇数
default:
    print("其他")
}

prefix operator ~>
prefix operator ~>=
prefix operator ~<
prefix operator ~<=
prefix func ~> (_ i: Int) -> ((Int) -> Bool) {
    {
        $0 > i
    }
}
prefix func ~>= (_ i: Int) -> ((Int) -> Bool) {
    {
        $0 >= i
    }
}
prefix func ~< (_ i: Int) -> ((Int) -> Bool) {
    {
        $0 < i
    }
}
prefix func ~<= (_ i: Int) -> ((Int) -> Bool) {
    {
        $0 <= i
    }
}
var n = 9
switch n {
case ~>=0:
    print("1") //1
case ~>=10:
    print("2")
default:
    break
}

//13.where 在5种情况下的使用
var data = (10, "Jack")
switch data {
case let (age, _) where age > 10:
    print(data.1, "age > 10")
case let (age, _) where age > 0:
    print(data.1, "age > 0") //Jack age > 0
default:
    break
}

var agess = [10, 20, 44, 23, 55]
for age in agess where age > 30 {
    print(age) //44 //55
}

protocol Stackable {
    associatedtype Element
}
protocol Container {
    associatedtype Stack : Stackable where Stack.Element : Equatable
}

func equal<S1: Stackable, S2: Stackable>(_ s1: S1, _s2: S2) -> Bool where S1.Element == S2.Element, S1.Element : Hashable {
    return false
}

extension Container where Self.Stack.Element : Hashable {}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值