简体   繁体   中英

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. 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.

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);
  1. Use an NSDateFormatter to convert your string into an NSDate .
  2. Use the [NSCalendar currentCalendar] to extract various date components (like the hour, minute, etc).

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. Also, your time range is a bit weird (maybe a typo?) - don't you want all 24 hours to be available?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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