简体   繁体   English

如何将时间值的NSString表示形式转换为包含小时和分钟的两个NSInteger形式?

[英]How can I convert a NSString representation of a time value into two NSInteger's containing the hour and minute?

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. 我将深入研究iOS开发和Objective C语言,并构建一个闹钟应用程序以熟悉SDK和语言。 I have an NSString object that represents a time, with the range "1:00 am" to "12:59 am" . 我有一个表示时间的NSString对象,范围为"1:00 am""12:59 am" I need to convert this NSString into two NSInteger 's that contain the hour value and minute value. 我需要将此NSString转换为两个包含小时值和分钟值的NSInteger 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操作非常费力,感觉就像是草率的代码。

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? 是否有一种简单的方法可以从时间值的NSString表示中提取小时和分钟字符,并将其数值存储在两个NSInteger

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);
  1. Use an NSDateFormatter to convert your string into an NSDate . 使用NSDateFormatter将您的字符串转换为NSDate
  2. Use the [NSCalendar currentCalendar] to extract various date components (like the hour, minute, etc). 使用[NSCalendar currentCalendar] 提取各种日期成分 (例如小时,分钟等)。

In other words: 换一种说法:

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"h:m a"];
NSDate *date = [formatter dateFromString:@"12:59 pm"];
[formatter release];

NSDateComponents * components = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];
NSLog(@"hour: %d", [components hour]);
NSLog(@"minute: %d", [components minute]);

This is the official way, as I know it. 据我所知,这是官方的方式。 It's not pretty: 这不是很漂亮:

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
[dateFormatter setDateFormat:@"h:m a"];
NSDate *date = [dateFormatter dateFromString:@"12:34 am"];

[dateFormatter setDateFormat:@"h"];
NSString *hours = [dateFormatter stringFromDate:date];

[dateFormatter setDateFormat:@"m"];
NSString *minutes = [dateFormatter stringFromDate:date];

BUT the string fiddling way of doing it (look for : , look for space, ...), may give you more headaches on the long term. 但是字符串的摆弄方式(寻找: ,寻找空间,...)从长远来看可能会给您带来更多麻烦。

NSString *time = @"1:00 am";
NSString *removeam = [time stringByReplacingOccurencesOfString:@" am" withString:@""];
SString *removepm = [removeam stringByReplacingOccurencesOfString:@" pm" withString:@""];   
NSArray *timeArray = [removepm componentsSeparatedByString:@":"];
NSInteger *hour = [[timeArray objectAtIndex:0] intValue];
NSInteger *mins = [[timeArray objectAtIndex:1] intValue];

得到时间间隔并将其写为

duration.text = [NSString stringWithFormat:@"%d:%02d", (int)audioPlayer.duration / 60, (int)audioPlayer.duration % 60, nil];

If you're building an alarm clock app, you probably will want to look into the NSDate and NSDateFormatter classes, instead of trying to pull all those strings apart into integer types. 如果要构建闹钟应用程序,则可能需要研究NSDateNSDateFormatter类,而不是尝试将所有这些字符串分开为整数类型。 Also, your time range is a bit weird (maybe a typo?) - don't you want all 24 hours to be available? 另外,您的时间范围有点怪异(也许是拼写错误?)-您不希望所有24小时都可用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM