zoukankan      html  css  js  c++  java
  • Associated Values & enum

    it is sometimes useful to be able to store associated values of other types alongside these case values. This enables you to store additional custom information along with the case value, and permits this information to vary each time you use that case in your code.

    In Swift, an enumeration to define product barcodes of either type might look like this:

    1. enum Barcode {
    2. case upc(Int, Int, Int, Int)
    3. case qrCode(String)
    4. }

    This can be read as:

    “Define an enumeration type called Barcode, which can take either a value of upc with an associated value of type (IntIntIntInt), or a value of qrCode with an associated value of type String.”

    This definition does not provide any actual Int or String values—it just defines the type of associated values that Barcode constants and variables can store when they are equal to Barcode.upc or Barcode.qrCode.

    New barcodes can then be created using either type:

    1. var productBarcode = Barcode.upc(8, 85909, 51226, 3)

    This example creates a new variable called productBarcode and assigns it a value of Barcode.upc with an associated tuple value of (8, 85909, 51226, 3).

    The same product can be assigned a different type of barcode:

    1. productBarcode = .qrCode("ABCDEFGHIJKLMNOP")

    At this point, the original Barcode.upc and its integer values are replaced by the new Barcode.qrCode and its string value. Constants and variables of type Barcode can store either a .upc or a .qrCode (together with their associated values), but they can only store one of them at any given time.

    The different barcode types can be checked using a switch statement, as before. This time, however, the associated values can be extracted as part of the switch statement. You extract each associated value as a constant (with the let prefix) or a variable (with the var prefix) for use within the switch case’s body:

    1. switch productBarcode {
    2. case .upc(let numberSystem, let manufacturer, let product, let check):
    3. print("UPC: (numberSystem), (manufacturer), (product), (check).")
    4. case .qrCode(let productCode):
    5. print("QR code: (productCode).")
    6. }
    7. // Prints "QR code: ABCDEFGHIJKLMNOP."

    If all of the associated values for an enumeration case are extracted as constants, or if all are extracted as variables, you can place a single var or let annotation before the case name, for brevity:

    1. switch productBarcode {
    2. case let .upc(numberSystem, manufacturer, product, check):
    3. print("UPC : (numberSystem), (manufacturer), (product), (check).")
    4. case let .qrCode(productCode):
    5. print("QR code: (productCode).")
    6. }
    7. // Prints "QR code: ABCDEFGHIJKLMNOP."
  • 相关阅读:
    解决在QEMU上仿真STM32F429时出现的若干问题
    CentOS 7.1, 7.2 下安装dotnet core
    [尝鲜]妈妈再也不用担心 dotnet core 程序发布了: .NET Core Global Tools
    程序员节应该写博客之.NET下使用HTTP请求的正确姿势
    [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [五] 如何做全站采集?
    [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [一] 初衷与架构设计
    ubuntu15.10 或者 16.04 或者 ElementryOS 下使用 Dotnet Core
    解决 docker on windows下网络不通
    Orchestrator中 errant 的判断
    golang 中时间差的计算
  • 原文地址:https://www.cnblogs.com/feng9exe/p/9073271.html
Copyright © 2011-2022 走看看