How can I convert a NSString representation of a t

2020-03-07 08:36发布

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

6条回答
贪生不怕死
2楼-- · 2020-03-07 08:54

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.

查看更多
3楼-- · 2020-03-07 08:54

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?

查看更多
啃猪蹄的小仙女
4楼-- · 2020-03-07 09:06
  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]);
查看更多
姐就是有狂的资本
5楼-- · 2020-03-07 09:08

get the time interval and write it as

duration.text = [NSString stringWithFormat:@"%d:%02d", (int)audioPlayer.duration / 60, (int)audioPlayer.duration % 60, nil];
查看更多
你好瞎i
6楼-- · 2020-03-07 09:11
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];
查看更多
家丑人穷心不美
7楼-- · 2020-03-07 09:12
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);
查看更多
登录 后发表回答