swift的enum基础
其它语言的枚举:
符号化的整型常量的集合;
swift的枚举:
可以是任何基础类型和无类型;
If you are familiar with C, you will know that C enumerations assign related names to a set of integer values. Enumerations in Swift are much more flexible, and don’t have to provide a value for each case of the enumeration. If a value (known as a raw value) is provided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type.
一、enum的类型:
enum defaultRawValue:String{}
enum TestEnum{
case goFun(para:String)
case doFun(para:String, para1:String)
}
二、rawValue:
具有类型的枚举对象具有rawValue;
数值类型的缺省值为:0、1 …
字符串类型的缺省rawValue为字符串本身;
三、具有关联值的枚举
- enum Barcode {
- case upc(Int, Int, Int, Int)
- case qrCode(String)
- }
“Define an enumeration type called Barcode, which can take either a value of upc with an associated value of type (Int, Int, Int, Int), or a value of qrCode with an associated value of type String.”
要区别枚举变量和关联值
枚举变量参与枚举运算;
关联值和rawvalue不参与。
四、枚举的初始化
1、非关联类型的枚举变量可以使用枚举值直接初始化;
2、有rawValue的枚举类型变量可以使用let possiblePlanet = Planet(rawValue: 7) 初始化;
3、有关联值的枚举值需要先对枚举值本身初始化,然后执行枚举变量初始化;
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
五、枚举使用:
1、普通使用switch;
2、有关联值的使用:
- switch productBarcode {
- case let .upc(numberSystem, manufacturer, product, check):
- print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")
- case let .qrCode(productCode):
- print("QR code: \(productCode).")
- }
3、if语句使用(赋值):条件选择使用
if case let .stream(inputStream, _) = uploadable {
upload.delegate.taskNeedNewBodyStream = { _, _ in inputStream }
}
if case .pending = box.inspect() {
print("PromiseKit: warning: pending promise deallocated")
}
swift的enum基础的更多相关文章
- iOS开发swift语法0基础篇—————(swift技术交流群:361513739)
iOS开发之swift语法0基础篇:点击打开链接 swift技术交流QQ群361513739
- swift第一季基础语法
同: 一.基础 同: 1常量和变量 2数据类型和数据类型转换 3别名 不同: 1可选类型optional 2BOOL类型 3元组类型 4断言Assertion 二.基本操作符 同: 1赋值和算术运算及 ...
- Swift 学习- 02 -- 基础部分2
class NamedShape{ var numberOfSides: Int = 0 var name: String init(name: String) { self.name = name ...
- swift的enum模式—对Alamofire入口的解析--数据结构与操作结合的模式
swift的枚举模式是数据结构与操作结合的模式 1.enum本质是一个类型,可以定义变量: 它定义的变量可以用到其它变量用的的任何地方:函数的输入.输出.成员变量.临时变量等: 这个变量还可以带有附加 ...
- swift 第一课 基础知识-1
1. 基本的打印: print("这是学习 swift 的第一课") var varA = print("a 的值是 \(varA)") //注:字符串打印参数 ...
- Swift语言的基础详解
1.1.关于swift · Swift 是一种适用于 iOS 和 OS X 应用的全新编程语言,它建立在最好的 C 和 Objective-C 语言之上,并且没有 C 语言的兼容性限制.Swift 采 ...
- Swift的一些基础内容
//①判断字符串是否为空的方法 isEmpty var str:String = "www.baidu.com" if str.isEmpty { print("空字符串 ...
- Enum基础
enum ColorE { RED, GREEN, BLUE; } public class GetEnumContent { public static void main(Stri ...
- 对于Swift的Enum,文档上没有说的
今天无意发现一个东西, 但是在文档上看了很多遍都没找到, 但是亲测是可行的, 那到底是什么呢? 以前我们定义枚举 会这样: enum Hello { case Item( String, Int) c ...
随机推荐
- sql视图中写case判断null值
下面是正解 用 is null (case when dbo.Feedback.Funnel is null then '否' when dbo.Feedback.Funnel='否' then '是 ...
- 【转】类找不到总结java.lang.ClassNotFoundException
(1)org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.sqls ...
- 修改input被选中的默认样式
input:focus{ outline: none; border: 1px solid #fff; } 或者 input[type=text]:focus{ outline: n ...
- package.json参数简单介绍
概述: 每个项目的根目录下都会有一个package.json文件,定义了项目所需的模块,以及项目信息.执行npm install 命令会自动下载package.json中配置的模块,也就是配置项目的运 ...
- 12:计算2的N次方
12:计算2的N次方 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 任意给定一个正整数N(N<=100),计算2的n次方的值. 输入 输入一个正整数N ...
- jQuery基础(动画篇 animate,显示隐藏,淡入淡出,下拉切换)
1.jQuery中隐藏元素的hide方法 让页面上的元素不可见,一般可以通过设置css的display为none属性.但是通过css直接修改是静态的布局,如果在代码执行的时候,一般是通过js控制元 ...
- ci 3.0 默认路由放在子文件夹 无法访问的解决办法
比方说你想配置默认路由为: $route['default_controller'] = 'index/home'; ci3.0之前是可以放在 controllers中的子文件夹中的,但是到了ci ...
- jquery 给iframe里的元素添加事件
$("#iframeId").on("load", function(event){//判断 iframe是否加载完成 这一步很重要 $("#divI ...
- 【小程序】返回顶部wx.pageScrollTo和scroll-view的对比
一.wx.pageScrollTo(https://mp.weixin.qq.com/debug/wxadoc/dev/api/scroll.html) 1. 小程序中双击顶部的textbar.会默认 ...
- 云数据库HBase助力物联网,免费申请中
云数据库HBase免费申请地址:https://cn.aliyun.com/product/hbase 引言 从有线互联网到无线互联网,本质是加强了人与人之间随时随地的关联.下一个互联的时代是万物互联 ...