前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >24. Swift学习之where关键字

24. Swift学习之where关键字

作者头像
YungFan
发布2018-12-14 16:32:50
8880
发布2018-12-14 16:32:50
举报
文章被收录于专栏:学海无涯学海无涯

在Swift中很多地方都是用到了一个关键字where,这个关键字的含义和数据库中的where差不多,用于条件筛选(条件过滤),那么在Swift中哪些地方用到了这个关键字呢?下面来总结一下。

一、 switch case中
var value = 60
switch value {
case let x where x < 60:
    print("不及格")
default:
    print("及格")
}
二、for循环中
let array = [0,1,2,3,4,5,6,7,8,9]
for value in array where value % 2 == 0 {
    print(value)
}
三、protocol协议中
protocol SomeProtocol{
    
}
extension SomeProtocol where Self:UIView{
    //只给遵守myProtocol协议的UIView添加了拓展
    func getInfo() -> String{
        return "属于UIView类型"
    }
}
extension UIButton:SomeProtocol{}
UIButton().getInfo()
四、Generic 泛型中
protocol AnOthorProtocol {
    func say()
}
struct Bird: AnOthorProtocol {
    func say() {
        print("Hello World")
    }
    
}
func genericFunc<T>(num: T) where T: AnOthorProtocol {
    print(num)
}
genericFunc(num: Bird())
五、do catch异常处理中
enum ExceptionError:Error{
    case httpCode(Int)
}
func throwError() throws {
    throw ExceptionError.httpCode(404)
}
do{
    try throwError()
}catch ExceptionError.httpCode(let httpCode) where httpCode == 404{
    print("not found error")
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、 switch case中
  • 二、for循环中
  • 三、protocol协议中
  • 四、Generic 泛型中
  • 五、do catch异常处理中
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档