美文网首页
Swift枚举类型

Swift枚举类型

作者: 古月思吉 | 来源:发表于2018-08-10 18:13 被阅读0次

1.基本枚举类型定义:

enum BookOrientationType: Int {
    case portrait
    case landscape
}
  • 这是比较常用的枚举类型定义,适用于大部分场景。但是枚举类型中的值除了一部分公共的参数外,可能还含有独有的元素、属性。而我们希望能够通过一个公共的方法实现不同枚举类型class的创建,这时候可以定义带参数的枚举类型。

2.带参数的枚举类型定义:

//定义
public enum HUDContentType {
    case success
    case error
    case progress
    case image(UIImage?)
    case rotatingImage(UIImage?)

    case labeledSuccess(title: String?, subtitle: String?)
    case labeledError(title: String?, subtitle: String?)
    case labeledProgress(title: String?, subtitle: String?)
    case labeledImage(image: UIImage?, title: String?, subtitle: String?)
    case labeledRotatingImage(image: UIImage?, title: String?, subtitle: String?)

    case label(String?)
    case systemActivity
}

//使用
switch content {
        case .success:
            return PKHUDSuccessView()
        case .error:
            return PKHUDErrorView()
        case .progress:
            return PKHUDProgressView()
        case let .image(image):
            return PKHUDSquareBaseView(image: image)
        case let .rotatingImage(image):
            return PKHUDRotatingImageView(image: image)

        case let .labeledSuccess(title, subtitle):
            return PKHUDSuccessView(title: title, subtitle: subtitle)
        case let .labeledError(title, subtitle):
            return PKHUDErrorView(title: title, subtitle: subtitle)
        case let .labeledProgress(title, subtitle):
            return PKHUDProgressView(title: title, subtitle: subtitle)
        case let .labeledImage(image, title, subtitle):
            return PKHUDSquareBaseView(image: image, title: title, subtitle: subtitle)
        case let .labeledRotatingImage(image, title, subtitle):
            return PKHUDRotatingImageView(image: image, title: title, subtitle: subtitle)

        case let .label(text):
            return PKHUDTextView(text: text)
        case .systemActivity:
            return PKHUDSystemActivityIndicatorView()
        }

相关文章

  • Swift 基础笔记 - 枚举

    枚举 OC定义和使用枚举 Swift定义枚举类型 Swift判断枚举类型 枚举成员类型

  • Swift - 枚举

    Swift - 枚举 枚举在C和Swift中有所不同, Swift中的枚举, 更加灵活, 更加强大 C 值类型: ...

  • swift4.1 系统学习十二 枚举

    /*枚举 swift的枚举类型也是一个很强大的类型,与其他语言相比,swift中枚举应用更加灵活、广泛。 例如:像...

  • Swift基础语法-枚举,枚举原始值,枚举相关值,switch提

    1. Swift枚举基本概念 Swift中的枚举比OC中的枚举强大, 因为Swift中的枚举是一等类型, 它可以像...

  • swift3.0- 枚举

    Swift枚举 Swift中的枚举比OC中的枚举强大, 因为Swift中的枚举是一等类型, 它可以像类和结构体一样...

  • 枚举

    Swift枚举:Swift中的枚举比OC中的枚举强大, 因为Swift中的枚举是一等类型, 它可以像类和结构体一样...

  • 从零学习Swift 02:枚举和可选项

    一:枚举 枚举的定义Swift中的枚举定义枚举类型很简单: 同 OC不同的是,Swift中的枚举不单单可以是Int...

  • Swift-07:枚举enum

    这里先介绍一下swift枚举与oc枚举的区别 OC: 枚举只能为Int类型swift : 可以在枚举中定义方法 可...

  • swift学习之枚举

    相比OC的枚举类型,swift的枚举类型就方便的多,OC的枚举类型成员属性只能是整数NSInterger(C语言的...

  • 每日一问17——swift基础(03)

    枚举类型 swift中使用enum关键字声明枚举。并且可以指定枚举的类型 方式一、 方式一枚举类型甚至可以为Str...

网友评论

      本文标题:Swift枚举类型

      本文链接:https://www.haomeiwen.com/subject/dtylbftx.html