Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何添加两个字符串,字符串有时间间隔?_Ios_Objective C_Nsstring_Nstimeinterval - Fatal编程技术网

Ios 如何添加两个字符串,字符串有时间间隔?

Ios 如何添加两个字符串,字符串有时间间隔?,ios,objective-c,nsstring,nstimeinterval,Ios,Objective C,Nsstring,Nstimeinterval,我有两个带时间间隔数据的字符串。我想将这两个时间间隔相加并存储到另一个字符串中。这是我的数据 NSString *oldTime=@"00:24"; //Time format is "mm:ss" NSString *newTime=@"00:07"; //Time format is "mm:ss" 添加两个字符串后,我需要将总时间存储在另一个字符串中,如下所示 NSString *totalTime=oldTime+newTime; //

我有两个带时间间隔数据的字符串。我想将这两个时间间隔相加并存储到另一个字符串中。这是我的数据

NSString *oldTime=@"00:24";         //Time format is "mm:ss"


NSString *newTime=@"00:07";         //Time format is "mm:ss"
添加两个字符串后,我需要将总时间存储在另一个字符串中,如下所示

NSString *totalTime=oldTime+newTime;        //(totalTime=00:31)  

我对iOS比较熟悉,所以请将此问题的代码发送给我。提前谢谢

我不知道这是完美的解决方案。但这会奏效的

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"mm:ss"];

    NSDate *date = [df dateFromString:@"00:00"];
    NSDate *date1 = [df dateFromString:@"00:24"];
    NSDate *date2 = [df dateFromString:@"00:07"];

    NSTimeInterval interval1 = [date1 timeIntervalSinceDate:date];
    NSTimeInterval interval2 = [date2 timeIntervalSinceDate:date];

    NSDate *addedDate = [date dateByAddingTimeInterval:interval1+interval2];

    NSString *resultDate = [df stringFromDate:addedDate];

希望这有帮助。

您需要学习逻辑。通过实践发展你的逻辑。