盘点 swift 中 where 关键字的所有用法,你知道多少?

关注我,每天分享一个关于 iOS 的新知识

前言

where 是 Swift 中一个强大的关键字,可以轻松过滤掉一些值。它可以用于许多不同的表达式中,今天就来盘点一下。

在 switch 中使用

switch 语句中的 case 中,可以用 where 关键字做二次筛选,举个例子:

swift 复制代码
let point = (x: 3, y: 4)

switch point {
case let (x, y) where x == y:
    print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
    print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
    print("(\(x), \(y)) is just some arbitrary point")
}

在这个示例中,我们有一个点的坐标点 (x, y)。在 switchcase 中,我们使用了 where 子句来判断点是否在 x == y 这条直线上,或是在 x == -y 这条直线上。

如果不满足 where 子句的条件,就会执行默认的 case 分支。

最后的输出为:(3, 4) is just some arbitrary point

在 for 循环中的使用

在 for 循环中,也可以使用 where 子句来添加条件判断:

typescript 复制代码
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers where number % 2 == 0 {
    print(number)
}

// 输出:
// 2
// 4
// 6
// 8
// 10

在这个例子中,我们遍历数字数组,但只打印出能被 2 整除的偶数。

在协议扩展中的用法

我们都知道,在 swift 中可以通过为协议添加扩展 (protocol extension) 来实现协议的默认实现。此时可以在扩展的 where 子句中添加约束:

scss 复制代码
protocol Shape {
    func draw()
}

extension Shape where Self: Equilateral {
    func draw() {
        // 绘制等边形
    }
}

extension Shape where Self: Rectangle {
    func draw() {
        // 绘制矩形
    }
}

struct Square: Shape, Equilateral { }

let square = Square()
square.draw() // 绘制等边形

我们为 Shape 协议添加了两个扩展,其中为 EquilateralRectangle 添加了 draw 方法的默认实现。

在扩展的 where 子句中指定了 Self 的约束类型。然后 Square 结构体实现了 ShapeEquilateral 协议,此时会调用 Equilateraldraw 实现。

通过 where 子句,可以根据协议扩展的指定类型进行有条件的默认实现,使协议扩展更加灵活。

各种高阶函数中的用法

1、查找满足条件的第一个元素

typescript 复制代码
let numbers = [1, 2, 3, 4, 5]
let number = numbers.first(where: { $0 > 2 })
print(number!) // 输出: 3

2、查找满足条件的最后一个元素

typescript 复制代码
let numbers = [1, 2, 3, 4, 5]
let number = numbers.last(where: { $0 > 2 })
print(number!) // 输出: 5

3、判断是否包含满足条件的元素

bash 复制代码
let numbers = [1, 2, 3, 4, 5]
let isContains = numbers.contains(where: { $0 > 2 })
print(isContains) // 输出: true

4、查找满足条件的第一个元素的下标

bash 复制代码
let numbers = [1, 2, 3, 4, 5]
let index = numbers.firstIndex(where: { $0 > 2 })
print(index!) // 输出: 2

5、查找满足条件的最后一个元素的下标

bash 复制代码
let numbers = [1, 2, 3, 4, 5]
let index = numbers.lastIndex(where: { $0 > 2 })
print(index!) // 输出: 4

6、将数组按条件拆分

ini 复制代码
let numbers = [1, 2, 3, 4, 5]
let array = numbers.split(whereSeparator: { $0 > 3 || $0 < 2 } )
print(array) // 输出: [ArraySlice([2, 3])]

在泛型函数中的用法

可以用 where 关键字向泛型参数添加约束:

swift 复制代码
protocol Printable {
    func print()
}

func printBoth<P1: Printable, P2: Printable>(_ p1: P1, _ p2: P2) where P1 == P2 {
    p1.print()
    p2.print()
}

在协议声明中的使用

在协议的声明中,可以为关联类型 (associatedtype) 的类型增加约束:

swift 复制代码
protocol Stack {
  associatedtype Element where Element: CustomStringConvertible
  mutating func push (_ element: Element)
  mutating func pop () -> Element?
}

这里每天分享一个 iOS 的新知识,快来关注我吧

本文同步自微信公众号 "iOS新知",每天准时分享一个新知识,这里只是同步,想要及时学到就来关注我吧!

相关推荐
AL.千灯学长6 小时前
DeepSeek接入Siri(已升级支持苹果手表)完整版硅基流动DeepSeek-R1部署
人工智能·gpt·ios·ai·苹果vision pro
openinstall全渠道统计1 天前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos
早起的年轻人1 天前
Flutter CupertinoNavigationBar iOS 风格导航栏的组件
flutter·ios
貂蝉空大1 天前
uni-app开发安卓和ios app 真机调试
android·ios·uni-app
胖虎11 天前
iOS 中的圆角与平滑圆角:从新特性到老项目适配
ios·圆角·平滑圆角·cornercurve
志飞1 天前
ios UICollectionView使用自定义UICollectionViewCell
ios·collectionview·自定义cell
Neo Evolution2 天前
Flutter与移动开发的未来:谷歌的技术愿景与实现路径
android·人工智能·学习·ios·前端框架·webview·着色器
没头脑的ht2 天前
ios App的启动过程和启动优化
ios
敲代码的鱼哇2 天前
设备唯一ID获取,支持安卓/iOS/鸿蒙Next(uni-device-id)UTS插件
android·ios·uniapp·harmonyos
江上清风山间明月3 天前
Flutter最简单的路由管理方式Navigator
android·flutter·ios·路由·页面管理·navigator