Swift 中的 where 关键字

在 Swift 中,where 关键字用于添加额外的约束条件,增强代码的精确性和安全性。以下是常见应用场景及示例代码:


1. 泛型类型约束

为泛型类型添加协议或类型要求:

func process<T>(_ value: T) where T: Equatable {
    if value == value { // 可用 == 操作符
        print("值相等")
    }
}

2. 协议扩展条件约束

为特定条件下的协议提供默认实现:

extension Collection where Element: Numeric {
    func sum() -> Element {
        return reduce(0, +)
    }
}

let numbers = [1, 2, 3]
print(numbers.sum()) // 输出:6

3. 循环中的条件过滤

for-in 循环中动态过滤元素:

let scores = [75, 42, 98, 53]
for score in scores where score >= 60 {
    print("及格分数:\(score)")
}
// 输出:75, 98

4. Switch 语句的额外条件

switchcase 中添加逻辑判断:

let point = (2, 2)
switch point {
case (let x, let y) where x == y:
    print("点在 y = x 对角线上")
default:
    break
}
// 输出:点在 y = x 对角线上

5. 关联类型的协议约束

约束协议关联类型的类型要求:

protocol Container {
    associatedtype Item
    func add(_ item: Item)
}

extension Container where Item: Equatable {
    func contains(_ target: Item) -> Bool {
        // 实现比较逻辑(此处简化)
        return true
    }
}

6. 类型别名约束

为类型别名添加泛型约束:

typealias NumericDictionary<Value> = [String: Value] 
    where Value: Numeric

let dict: NumericDictionary<Int> = ["age": 30]

7. 错误处理的模式匹配

catch 中捕获特定错误:

enum NetworkError: Error {
    case timeout(duration: Int)
    case invalidResponse(code: Int)
}

do {
    throw NetworkError.timeout(duration: 30)
} catch NetworkError.timeout(let duration) where duration > 20 {
    print("严重超时:\(duration)秒")
} catch {
    print("其他错误")
}
// 输出:严重超时:30秒

8. 可选绑定的条件检查

if letguard let 中添加额外条件:

func loadImage(name: String?) {
    guard let filename = name, 
          !filename.isEmpty else {
        return
    }
    print("加载图片: \(filename)")
}

loadImage(name: "avatar.png") // 成功执行
loadImage(name: "")           // 被 guard 拦截

关键点总结:

场景 作用 典型代码模式
泛型函数/类型 添加类型约束 func foo<T>() where T: Protocol
协议扩展 限定扩展生效范围 extension Proto where T: Type
循环控制 动态过滤元素 for x in array where condition
Switch 语句 增强模式匹配 case pattern where condition
错误处理 精确捕获错误 catch error where condition
可选绑定 绑定后立即检查条件 if let x = opt where x > 0

通过合理使用 where,可以显著提升代码的类型安全性和表达力,减少冗余的类型检查逻辑。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 以下是 Swift 中常用关键字的分类及具体示例,特别补充了 Swift 并发相关内容,帮助理解每个关键字的使用场...
    Gabriella0629阅读 412评论 0 0
  • 在 Swift 中,`in` 关键字有多种用途,主要用于控制流、闭包和协议等。以下是一些常见的使用场景和示例: #...
    me_967d阅读 1,072评论 0 0
  • 作者:Jordan Morgan,原文链接,原文日期:2017-02-11 译者:郑一一;校对:numbbbbb,...
    iOS亮子阅读 3,795评论 0 1
  • Swift关键字汇总如下: 一: 常见使用频繁的关键字: 1,访问修饰符:在swift中,访问修饰符有五种,分别是...
    物非0人非阅读 8,871评论 2 15
  • 模式匹配 模式匹配是 Swift 中非常常见的一种编程模式,使用模式匹配,可以帮助我们写出简明、清晰以及易读的代码...
    devedbox阅读 3,624评论 0 1