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

[已解决]swift中实现enum中赋值

Swift crifan 2549浏览 0评论

想要实现:

    enum FileType{
        case Other = "other"
        case Video = "video"
        case Image = "image"
        case Audio = "audio"
        case Apk = "apk"
        case Ipa = "ipa"
        case Mpp = "mpp"
        case Pdf = "pdf"
        case Ppt = "ppt"
        case Rar = "rar"
        case txt = "txt"
        case Vsd = "vsd"
        case Word = "word"
        case Xls = "xls"
    }

结果出错:

swift – How to enumerate an enum with String type? – Stack Overflow

后来用:

enum FileType: String {
    case Other = "other"
    case Video = "video"
    case Image = "image"
    case Audio = "audio"
    case Apk = "apk"
    case Ipa = "ipa"
    case Mpp = "mpp"
    case Pdf = "pdf"
    case Ppt = "ppt"
    case Rar = "rar"
    case Txt = "txt"
    case Vsd = "vsd"
    case Word = "word"
    case Xls = "xls"
}

后续还可以转换为字符串:

    func getFileIconFromType(fileType:FileType) -> UIImage {
        let typeName:String = String(fileType).lowercaseString
        print("typeName=\(typeName)") //xls
        let iconName = "file_icon_large_" + typeName
        print("iconName=\(iconName)") //file_icon_large_xls
        let fileIcon:UIImage = UIImage(named: iconName)!
        print("fileIcon=\(fileIcon)")
        return fileIcon
    }

即可。

转载请注明:在路上 » [已解决]swift中实现enum中赋值

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.205 seconds, using 22.11MB memory