如何将时间值的NSString表示转换为包含小时和分钟的两个NSInteger? [英] How can I convert a NSString representation of a time value into two NSInteger's containing the hour and minute?

查看:79
本文介绍了如何将时间值的NSString表示转换为包含小时和分钟的两个NSInteger?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在深入研究iOS开发和Objective C语言,并正在构建一个闹钟应用程序,以熟悉SDK和语言。我有一个 NSString 对象代表一个时间,范围凌晨1点上午12:59。我需要将此 NSString 转换为包含小时值和分钟值的两个 NSInteger 。在我这样做的时候,我发现我正在做的 NSString 操作非常费力,而且感觉就像是草率的代码。

I'm diving into iOS development and the Objective C language and am building an alarm clock app to become familiar with the SDK and language. I have an NSString object that represents a time, with the range "1:00 am" to "12:59 am". I need to convert this NSString into two NSInteger's that contain the hour value and minute value. As I'm doing this, I'm finding the NSString manipulation that I'm doing to be extremely laborious and it just feels like sloppy code.

是否有一种简单的方法可以从时间值和商店的 NSString 表示中提取小时和分钟字符他们的数值是两个 NSInteger 的?

Is there a simple way to extract the hour and minute characters from a NSString representation of a time value and store their numerical values in two NSInteger's?

提前感谢您的帮助!我要回到它...

Thanks in advance for all your help! I'm gonna get back to it...

推荐答案

NSScanner* timeScanner=[NSScanner scannerWithString:...the time string...];
int hours,minutes;
[timeScanner scanInt:&hours];
[timeScanner scanString:@":" intoString:nil]; //jump over :
[timeScanner scanInt:&minutes];

NSLog(@"hours:%d minutes:%d",hours,minutes);

这篇关于如何将时间值的NSString表示转换为包含小时和分钟的两个NSInteger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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