“ios转换字符串”;5小时10分钟”;转换为整数小时和分钟

“ios转换字符串”;5小时10分钟”;转换为整数小时和分钟,ios,objective-c,nsstring,Ios,Objective C,Nsstring,我正在尝试从字符串“5小时10分钟”中获取整数变量中的小时和分钟。有人能为我提供解决方案吗 NSString *duration =@"5 hours 10 min"; int hours = 5 int min = 10 试试这个 - (NSString *)timeFormatted:(int)totalSeconds { int seconds = totalSeconds % 60; int minutes = (totalSeconds / 60) % 60;

我正在尝试从字符串“5小时10分钟”中获取整数变量中的小时和分钟。有人能为我提供解决方案吗

NSString *duration =@"5 hours 10 min";

int hours = 5
int min = 10
试试这个

- (NSString *)timeFormatted:(int)totalSeconds
{

    int seconds = totalSeconds % 60; 
    int minutes = (totalSeconds / 60) % 60; 
    int hours = totalSeconds / 3600; 

    return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds]; 
}

请尝试以下代码以检索int值

@try {
    NSString *myString = @"5 hours 10 min";
    NSArray *myWords = [myString componentsSeparatedByCharactersInSet:
                        [NSCharacterSet characterSetWithCharactersInString:@" "]
                        ];
    int hoursValue = 0, minuteValue = 0;
    hoursValue = [[myWords objectAtIndex:0] intValue];
    if([myWords count] > 2) {
        minuteValue = [[myWords objectAtIndex:2] intValue];
    }

    NSLog(@"hours %d and Minute %d",hoursValue,minuteValue);
 }
 @catch (NSException * e) {
    NSLog(@"Exception: %@", e);
 }
试试这个

    @try
    {
           NSString *myString = @"5 hours 10 min";
           NSArray *myWords = [myString componentsSeparatedByString:@" "];
           int hoursValue = 0, minuteValue = 0;
           if ([myWords count] == 4)
           {
                hoursValue = [[myWords objectAtIndex:0] intValue];
                minuteValue = [[myWords objectAtIndex:2] intValue];

           }
           else if([myWords count] == 2)
           {
                if([[myWords objectAtIndex:1] isEqualToString:@"hours"] == yes)
                {
                    hoursValue = [[myWords objectAtIndex:0] intValue];
                }
                else
                {
                    minuteValue = [[myWords objectAtIndex:0] intValue];
                }
           }   
           NSLog(@"hours %d and Minute %d",hoursValue,minuteValue);
    }
    @catch(Exception *exception
    {
    }

使用NSDateFormatter将string1转换为NSDate,然后获取所需的NSDateComponents:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"<Date format>"];
    NSDate *date = [dateFormatter dateFromString:string1];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
    NSInteger hour = [components hour];
    NSInteger minute = [components minute];
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[日期格式化程序setDateFormat:@”“;
NSDate*date=[dateFormatter dateFromString:string1];
NSCalendar*日历=[NSCalendar currentCalendar];
NSDateComponents*components=[日历组件:(NSCalendarUnitHour | NSCalendarUnitMinute)fromDate:date];
NSInteger小时=[组件小时];
NSInteger分钟=[组件分钟];
了解更多信息

尝试以下方法:

int hr,mins;
NSString *str=@"5 hours 10 min";
NSArray *firstWords = [str componentsSeparatedByString:@" "];
if (([[firstWords objectAtIndex:1] isEqualToString:@"hours"]||[[firstWords objectAtIndex:1] isEqualToString:@"hr"] ) &&([[firstWords objectAtIndex:3] isEqualToString:@"minutes"]||[[firstWords objectAtIndex:3] isEqualToString:@"min"])) {
    hr=(int)[[firstWords objectAtIndex:0]integerValue];
    mins=(int)[[firstWords objectAtIndex:2]integerValue];
    if (mins > 60) {
        hr+=mins/60;
        mins    =mins%60;

    }
}

你想把它分成两个不同的变量吗?你尝试了什么?您是否尝试了NSDateFormatter?@Er.shryansh Shah Yesis此格式已修复?您能告诉我您想对您的答案做什么吗。这不属于问题。我知道,但这也是HH:MM:SS的一个好方法,可能就是为什么我给ans这么多的原因。请把这段代码放进去试试看,如果你的格式更改,它可能会使你的应用程序崩溃