最新消息:VPS服务器又从fzhost.net换回linode了,主题仍用朋友推荐的大前端D8

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

Swift crifan 105浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
20 queries in 0.199 seconds, using 9.75MB memory