如何使用swift枚举定义
发表于:2025-11-18 作者:千家信息网编辑
千家信息网最后更新 2025年11月18日,本篇内容主要讲解"如何使用swift枚举定义",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用swift枚举定义"吧!枚举定义//枚举定义enum C
千家信息网最后更新 2025年11月18日如何使用swift枚举定义
本篇内容主要讲解"如何使用swift枚举定义",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用swift枚举定义"吧!
枚举定义
//枚举定义enum CompassPoint { case north case south case east case west}//常配合switch case使用let directionToHead = CompassPoint.southswitch directionToHead {case .north: print("Lots of planets have a north")case .south: print("Watch out for penguins")case .east: print("Where the sun rises")case .west: print("Where the skies are blue")}/* Watch out for penguins */枚举遍历
//枚举遍历enum Planet: CaseIterable { case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune}for planet in Planet.allCases { print(planet)}/* mercury venus earth mars jupiter saturn uranus neptune */关联值
//关联值enum Barcode { case upc(Int, Int, Int, Int) case qrCode(String)}func showBarcode(_ barcode: Barcode) { switch barcode { case .upc(let numberSystem, let manufacturer, let product, let check): print("UPC:\(numberSystem), \(manufacturer), \(product), \(check)") case .qrCode(let productCode): print("QR Code: \(productCode)") }}var productBarcode = Barcode.upc(1, 1, 1, 1)showBarcode(productBarcode)//UPC:1, 1, 1, 1productBarcode = Barcode.qrCode("hello")showBarcode(productBarcode)//QR Code: hello原始值
//原始值enum ASCIIControlCharacter: Character { case tab = "\t" case lineFeed = "\n" case carriageReturn = "\r"}从原始值初始化
//从原始值初始化enum RoleStatus: Int,CaseIterable { case run case jump case walk case idle}for i in 0...RoleStatus.allCases.count { print(RoleStatus(rawValue: i))}/* Optional(__lldb_expr_8.RoleStatus.run) Optional(__lldb_expr_8.RoleStatus.jump) Optional(__lldb_expr_8.RoleStatus.walk) Optional(__lldb_expr_8.RoleStatus.idle) nil */递归枚举(indirect)
//递归枚举 (5+4)*2indirect enum ArithmeticExpression { case number(Int) case addition(ArithmeticExpression, ArithmeticExpression) case multiplication(ArithmeticExpression, ArithmeticExpression)}let five = ArithmeticExpression.number(5)let four = ArithmeticExpression.number(4)let sum = ArithmeticExpression.addition(five, four)let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))func evaluate(_ expression: ArithmeticExpression) -> Int { switch expression { case let .number(value): return value case let .addition(left, right): return evaluate(left) + evaluate(right) case let .multiplication(left, right): return evaluate(left) * evaluate(right) }}print(evaluate(product))//18到此,相信大家对"如何使用swift枚举定义"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
原始
内容
递归
关联
学习
实用
更深
兴趣
实用性
实际
操作简单
方法
更多
朋友
网站
频道
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
腾讯云设置服务器白名单
漳州艾泽网络技术
移动摄像头为什么连接不到服务器
北京软件开发服务商
数据库改什么名字好
服务器不能进入管理后台
山东常见软件开发哪家便宜
聊城app软件开发服务公司
华为服务器 管理地口
3011服务器崩溃
a4网络安全宣传海报
宜章计算机软件开发学费
企业绿色数据库
北京茂泡网络技术有限公司
中国青年网网络安全直播
知名冷库软件开发
新化天气预报软件开发
一起长大的软件开发商
网络安全主题作文800字
数据库字段 监听
mssql数据库位置
宜兴软件开发欢迎来电
国家网络安全曲
BJ天气预报软件开发
网络安全网站大全
磁贴数据库损坏如何修复
win7如何设置数据库
数据库工程师在广州薪资
网络安全未来的核心资产
mc国际服好玩的中国服务器