首页 > 其他分享 >【Swift 60秒】17 - Enum associated values

【Swift 60秒】17 - Enum associated values

时间:2022-10-24 18:08:07浏览次数:83  
标签:case Enum associated 17 enum talking running someone


0x00 Lesson

As well as storing a simple value, enums can also store ​​associated​​​ values attached to each case. This lets you attach ​​additional​​ information to your enums so they can represent more nuanced data.

For example, we might define an enum that stores various kinds of activities:

enum Activity
case bored
case running
case talking
case singing
}

That lets us say that someone is talking, but we don’t know what they talking about, or we can know that someone is running, but we don’t know where they are running to.

Enum associated values let us add those additional details:

enum Activity{
case bored
case running (destination: String)
case talking(topic: String)
case singing (volume: Int)
}

Now we can be more precise - we can say that someone is taking about football:

let talking = Activity.talking(topic: "footbal")

0x01 Tips

Associated values let you attach extra properties to any or all of your enum cases.


0x02 我的小作品

欢迎体验我的作品之一:​​小五笔​​​ 五笔学习好帮手
​App Store​​ 搜索即可~



标签:case,Enum,associated,17,enum,talking,running,someone
From: https://blog.51cto.com/u_15844020/5790736

相关文章

  • GCJ 2017 R2 题解(待续)
    ProblemA.FreshChocolateProblemYouarethepublicrelationsmanagerforachocolatemanufacturer.Unfortunately,thecompany’simagehassufferedbecausecus......
  • BZOJ 1797([Ahoi2009]Mincut 最小割-最小割的可行边与必行边)
    DescriptionA,B两个国家正在交战,其中A国的物资运输网中有N个中转站,M条单向道路。设其中第i(1≤i≤M)条道路连接了vi,ui两个中转站,那么中转站vi可以通过该道路到达ui中转站,......
  • BZOJ 4776([Usaco2017 Open]Modern Art-想法题)
    已知一个矩阵,初始全0,你每一次选一个非空子矩阵,涂上一个数。现在你涂n^2次,其中1~n^2每个数用一次,问哪个数可能是第一次涂的。我们预先框出每个数字涂的最小子矩阵,然后看看......
  • GCJ Qualification Round 2017 题解(部分)
    OversizedPancakeFlipper#include<bits/stdc++.h>usingnamespacestd;#define#define#define#define#define#define#define#define#define#define#define#define#define#......
  • ACM ICPC 2017 Warmup Contest 1 (NCPC 2016)
    AArtwork倒跑并查集#include<bits/stdc++.h>usingnamespacestd;#define#define#define#define#define#define#define#define#define#define#define#define#define#defin......
  • GCJ Round 1A 2017 题解
    AAlphabetCake给一个R*C矩阵,里面有大写字母和?(大写字母每个最多出现一次),用矩阵中出现的大写字母填满矩阵,要求每个字母出现的区域都恰为一子矩阵。直接把每个字母向行延展......
  • BZOJ 4810([Ynoi2017]由乃的玉米田-莫队)
    Description由乃在自己的农田边散步,她突然发现田里的一排玉米非常的不美。这排玉米一共有N株,它们的高度参差不齐。由乃认为玉米田不美,所以她决定出个数据结构题这个题是这......
  • Codeforces Round #829 (Div. 2)/CodeForces1754
    CodeForces1754注:所有代码均为场上所书TechnicalSupport解析:题目大意给定一个只包含大写字母\(\texttt{Q}\)和\(\texttt{A}\)的字符串,如果字符串里的每一个\(\t......
  • HTL6217系列可用5节 ~7节电池的串联连接
    概述HTL6217系列内置高精度电压检测电路和延迟电路,是用于锂离子可充电电池的二次保护IC。通过将各节电池间短路,可适用于5节 ~7节电池的串联连接。封装MSOP-10特点1、针对各......
  • 2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017)
    AAlienSunset模拟#include<bits/stdc++.h>usingnamespacestd;#define#define#define#define#define#define#define#define#define#define#define#define#define#define......