1

我正在开发一个需要时间戳的应用程序。我有两个数组,其中包含包含小时、分钟和秒的字符串对象,我需要减去这些数组并将它们作为小时、分钟和秒作为三个对象放入第三个数组中。

例子:

arr1=[9,10,22]
arr2=[10,12,42];

所以答案应该是

arr3=[1,2,22]

我需要把这个数组对象放在一个由分隔的字符串中' :'

请帮忙。

4

3 回答 3

0

试试这个

NSMutableArray * arr3=[[NSMutableArray alloc]init];

[arr3 addObject:[NSString stringWithFormat:@"%d",[[arr2 objectAtIndex:0]integerValue]-[[arr1 objectAtIndex:0]integerValue]]];

[arr3 addObject:[NSString stringWithFormat:@"%d",[[arr2 objectAtIndex:1]integerValue]-[[arr1 objectAtIndex:1]integerValue]]];

[arr3 addObject:[NSString stringWithFormat:@"%d",[[arr2 objectAtIndex:2]integerValue]-[[arr1 objectAtIndex:2]integerValue]]];
于 2012-09-03T05:29:10.587 回答
0

你也可以试试这个:

NSMutableArray *arr3=[[NSMutableArray alloc]init];

    [arr3 addObject:[NSString stringWithFormat:@"%d",[[arr2 objectAtIndex:0]intValue]-[[arr1 objectAtIndex:0]intValue]]];
    [arr3 addObject:[NSString stringWithFormat:@"%d",[[arr2 objectAtIndex:1]intValue]-[[arr1 objectAtIndex:1]intValue]]];
    [arr3 addObject:[NSString stringWithFormat:@"%d",[[arr2 objectAtIndex:2]intValue]-[[arr1 objectAtIndex:2]intValue]]];
于 2012-09-03T05:32:47.543 回答
0

试试这个:

NSMutableArray *arr3 = [NSMutableArray alloc]init];
for(int i=0;i<[arr1 count];i++)
{
  [arr3 addObject:[NSString stringWithFormat:@"%d",[[arr2 objectAtIndex:i]integerValue]-[[arr1 objectAtIndex:i]integerValue]]];
}

NSString *combinedStr = [arr3 componentsJoinedByString:@":"];
于 2012-09-03T05:35:22.717 回答