最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift枚举类型的继承

iOS crifan 6309浏览 0评论

对于文件的类型,想要实现main和sub的contentType的定义

enum MainContentType:String {
    case Unknown
    case Image
    case Audio
    case Video
    case Document
}
enum SubContentType:String {
    case Unknown
}
enum ImageType: SubContentType {
    case Gif
    case Icon
    case Jpeg //jpg, jpeg, jpe
    case Png
    case Tiff
}
class ContentType:NSObject, NSCoding {
    var mainType:MainContentType
    var subType:SubContentType
    var audioType:AudioType
    var videoType:VideoType
    var documentType:DocumentType

但是会出错:

/Users/crifan/dev/dev_root/daryun/Workhub/sourcecode/WorkhubiOS/Workhub/Workhub/Resource.swift:23:17: Raw type ‘SubContentType’ is not convertible from any literal

swift enum subclass

ios – Swift – Subclass an Enum (or something to that effect) – Stack Overflow

How in Swift specify type constraint to be enum? – Stack Overflow

Swift Enum Cheatsheet

Advanced & Practical Enum usage in Swift

好像可以用:

嵌套的枚举

取代枚举的继承

从而实现我要的效果

enum ContentType:String {
    case Unknown
    enum Image:String {
        case Unknown
        case Gif
        case Icon
        case Jpeg //jpg, jpeg, jpe
        case Png
        case Tiff
    }
    enum Audio:String {
        case Unknown
        case Mid
        case Mp3
        case Wav
        case Wma
    }
    enum Video:String {
        case Unknown
        case Asf
        case Avi
        case Mpeg //mp4, mpeg
        case Wmv
        //    case Rmvb ???
        //    case Vob ???
    }
    enum Document:String {
        case Unknown
        case Mpp
        case Pdf
        case Ppt
        case Txt
        case Vsd
        case Word
        case Xls
        case Html
    }
}

转载请注明:在路上 » [已解决]swift枚举类型的继承

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.212 seconds, using 22.06MB memory