首页 > 解决方案 > 如何根据数组中的某些条件用 Swift 中的常量减去每个 Int 值?

问题描述

我有一个数组 ["11-16,20-23", "11-16,20-23", "11-16,20-23"]

如何在 Swift 中减去大于 12 的该数组的 Int 值?所以数组变成 ["11-4,8-​​11", "11-4,8-​​11", "11-4,8-​​11"] 像这样

我目前的解决方法是

func test() {
        let hours = "11-16,20-23|11-16,20-23|11-16,20-23|11-16,20-23|13-17,20-24|11-16,20-23|11-16,20-23"
        var firstHalf : [String] = []
               var secondHalf : [String] = []
               let split = hours.components(separatedBy: "|")
               for i in 0..<split.count {
                   let index = split[i].components(separatedBy: ",")
                   firstHalf.append(index[0])
                   secondHalf.append(index[1])
               }
               print(firstHalf)
               print(secondHalf)
               
               let final  = firstHalf.first?.components(separatedBy: "-")
               print(final?[0])
               print(final?[1])
    }

标签: arraysswift

解决方案


可以使用.map. 首先,如果我们只是从数组中的数据开始

let data = ["11-16,20-23", "11-16,20-23", "11-16,20-23"]

我们需要执行以下步骤。

  1. 用“,”分割数组中的每个字符串
  2. 用“-”分割上述结果中的子字符串
  3. 检查该值是否大于 12,如果需要,减去 12
  4. 用“-”加入转换新值
  5. 用“,”连接上面的结果

当我们split在 a 上使用时,String我们得到以下内容[String.SubSequence],我们需要将其转换回 a[String]我们可以通过在执行.map { String($0) }之后立即执行 a 来做到这一点split

let data = ["11-16,20-23", "11-16,20-23", "11-16,20-23"]

let result = data.map { $0
    .split(separator: ",")          // split each String item at the comma
    .map { String($0) }             // convert to String as we have subsequences
    .map { $0
        .split(separator: "-")      // Now split each string by  the dash
        .map { String($0) }         // convert to String
        .map(convertTime)           // use the above convert time function
        .joined(separator: "-")     // join the values with a dash
    }
    .joined(separator: ",")         // join the values with a comma
}

func convertTime(_ hour: String) -> String {
    // When casting to Int we get Int? so we need to unwrap it
    if let value = Int(hour), value > 12 {
        return String(value - 12)
    } else {
        return hour
    }
}


print(result) // ["11-4,8-11", "11-4,8-11", "11-4,8-11"]

您可以通过使用使其更简单.components(separatedBy:)

let result = data.map { $0
    .components(separatedBy: ",")
    .map { $0
        .components(separatedBy: "-")       
        .map(convertTime)           
        .joined(separator: "-")     
    }
    .joined(separator: ",")         
}

更新

可以映射到星期几。我们需要一些东西。首先,我们需要确保数据的顺序与星期几的顺序相匹配。如果它们不匹配,或者没有以相同的方式一致地排序,那么就不可能映射它们。

其次,从您的评论看来,您希望将它们映射到自定义结构。所以我们需要创建结构

struct OpeningTimes {
    let day: String
    let fromWorkingHours: String
}

然后我们可以使用上面的答案输出更新的小时数,result然后我们可以用一周中的日期数组压缩它。为方便起见,我刚刚在下面的代码中使用了上面的输出,以便您有一个包含的示例。

let result = ["11-4,8-11", "11-4,8-11", "11-4,8-11", "11-4,8-11", "11-4,8-11", "11-4,8-11", "11-4,8-11"] 
let weekDays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

let arrayOfOpeningTimes = zip(weekDays, result).map {
    OpeningTimes(day: $0.0, fromWorkingHours: $0.1)
}


print(arrayOfOpeningTimes)

这将创建一个 数组OpeningTimes,只需确保在每个工作日和小时数组中具有相同数量的项目,否则将缺少值。


推荐阅读