-->

How can I convert a NSString representation of a t

2020-03-07 08:17发布

问题:

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

回答1:

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);


回答2:

  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]);


回答3:

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.



回答4:

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];


回答5:

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?



回答6:

get the time interval and write it as

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