将日期字符串转换为Int Swift [英] Convert Date String to Int Swift

查看:255
本文介绍了将日期字符串转换为Int Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换字符串:

I am trying to convert the string:

let time = "7:30"

转换为整数:

let hour : Int = 7
let minutes : Int = 30

我当前正在遍历字符串:

I am currently looping through the string:

for char in time.characters {
}

但是我不知道如何将char转换为int.任何帮助将不胜感激.

But I cannot figure out how to convert a char to an int. Any help would be greatly appreciated.

推荐答案

@alex_p和@mixel的答案是正确的,但也可以使用Swift split函数来实现:

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]

这篇关于将日期字符串转换为Int Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆