想要实现:
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中赋值