【问题标题】:How to get multiple values from a switch如何从开关中获取多个值
【发布时间】:2021-05-24 02:54:05
【问题描述】:

我得到了 Int 数组,这是电影流派,我需要为每个元素赋予相应的流派(例如:28 - “动作”,12 - “冒险”)并将它们显示在 tableView 单元格中。

我只能显示一种类型,而不是多种类型。能否请您帮我修复代码并就如何正确获取流派提出一些建议。

func getGenre(filmGenres: [Int]) -> String {
    
    var genre: String = "Action"
    
    for i in filmGenres {
        
        switch i {
        case 28:
            genre = "Action"
        case 12:
            genre = "Adventure"
        case 16:
            genre = "Animation"
        case 35:
            genre = "Comedy"
        case 80:
            genre = "Crime"
        case 99:
            genre = "Documentary"
        case 18:
            genre = "Drama"
        case 10751:
            genre = "Family"
        case 14:
            genre = "Fantasy"
        case 36:
            genre = "History"
        case 27:
            genre = "Horror"
        case 10402:
            genre = "Music"
        case 9648:
            genre = "Mystery"
        case 10749:
            genre = "Romance"
        case 878:
            genre = "Science Fiction"
        case 10770:
            genre = "TV Movie"
        case 53:
            genre = "Thriller"
        case 10752:
            genre = "War"
        case 37:
            genre = "Western"
        default:
            return ""
    }
    }
       return genre
}
cell.filmGenre.text = movie.getGenre(filmGenres: movie.genreIDs!) 

【问题讨论】:

  • 我猜你应该有一个参数而不是数组作为参数,因为你返回 1 个值而不是数组
  • 与其使用 Int,不如为您的电影类型定义自定义枚举。您可能想使用filmGenres.map(...)

标签: arrays swift switch-statement


【解决方案1】:

我认为拥抱枚举是一个很好的例子,你将拥有一个类型的枚举

enum Genre: Int {
  case Action = 28
  case Adventure = 12
  case Animation = 16
  //.. list all your cases

  var name: String {
     "\(self)"
  }
}

然后在你的逻辑中你可以有一些看起来像

let genres = genreIds.compactMap(Genre.init).map(\.name).joined(separator: ",")

干净且易于推理:)

【讨论】:

    【解决方案2】:

    据我了解,您希望从genreIDs 数组中获取一个包含所有流派的字符串。

    let genreIds = [28, 12, 99, 12]
    
    // Just to make sure we have unique genres. 
    // You may skip this if your application logic ensures this
    let uniqGenreIds = Array(Set(genreIds))  
    
    // Create an sorted array with the textual genre names:
    let stringGenres = uniqGenreIds.map {
        (intGenre) -> String in
        switch intGenre {
            case 28:
                return "Action"
            case 12:
                return "Adventure"
            case 16:
                return "Animation"
            default: return "unknown"
        }
    }.sorted()
    
    // Join the strings:
    let allGenres = stringGenres.joined(separator: ",")
    print (allGenres) // Action,Adventure,unknown
    

    【讨论】:

    • 非常感谢!出色的实施。现在我明白了如何从 switch 获取多个值。所以我们不能只用map,还要调用sort func。是否让 uniqGenreIds = Array(Set(genreIds)) 检查数组是否包含相同的值?
    • 是的,这些语句创建了一个 Set(它将丢弃重复项),然后从该不同的值创建一个新数组。
    猜你喜欢
    • 2020-08-10
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多