Convert Date String to Int Swift

后端 未结 3 1390
时光取名叫无心
时光取名叫无心 2021-01-19 14:39

I am trying to convert the string:

let time = \"7:30\"

to integers:

let hour : Int = 7
let minutes : Int = 30
3条回答
  •  日久生厌
    2021-01-19 15:24

    Answers by @alex_p and @mixel are correct, but it's also possible to do it with Swift split function:

    let time = "7:30"
    let components = time.characters.split { $0 == ":" } .map { (x) -> Int in return Int(String(x))! }
    
    let hours = components[0]
    let minutes = components[1]
    

提交回复
热议问题