我有一个带有
的目标C对象“ Obj”@property (strong, nonatomic) NSMutableDictionary *obj;
它将使用以下格式填充来自api的数据。
{
0 = ( 0, "7.00", "8.59”, ”15.00”, ”16.59", "19.00", "20.59 );
1 = ( 1, "7.00", "8.59”, ”15.00”, ”16.59", "19.00", "20.59 );
2 = ( 2, "7.00", "8.59”, ”15.00”, ”16.59", "19.00", "20.59 );
3 = ( 3, "7.00", "8.59”, ”15.00”, ”16.59", "19.00", "20.59 );
4 = ( 4, "7.00", "8.59”, ”15.00”, ”16.59", "19.00", "20.59 );
5 = ( 5, "7.00", "8.59”, ”15.00”, ”16.59", "19.00", "20.59 );
6 = ( 6, "7.00", "8.59”, ”15.00”, ”16.59", "19.00", "20.59 );
}
我需要解析数据并以以下格式将其作为数组发送回去。
`{
[ 0, 7, 00, 8, 59 ],
[0, 15, 00, 16, 59] ,
[0, 19, 00, 20, 59] ,
[ 1, 7, 00, 8, 59 ],
[1, 15, 00, 16, 59] ,
[1, 19, 00, 20, 59],
……
……
[ 6, 7, 00, 8, 59 ],
[6, 15, 00, 16, 59] ,
[6, 19, 00, 20, 59]
}`
其中0,1,2,3,4,5,6是天,其余信息是小时和分钟。因此,对于每个广告位,格式为
`[day, from_hour, from_min, to_hour, to_min],
[0, 7, 0, 8, 59],`
一天可能有多个时段,也可能只有一个时段。
到目前为止,我设法进行循环并按如下所示分隔每天的记录。
NSMutableArray *elementArray =[[NSMutableArray alloc ]init];
NSMutableArray *results =[[NSMutableArray alloc ]init];
NSInteger starthour = 0 ;
NSInteger startmin = 0 ;
NSInteger endhour = 0 ;
NSInteger endmin = 0 ;
NSMutableArray *temp = [[NSMutableArray alloc]init];
NSString *string;
int i = 0;
for ( i = 0; i <=6; i ++){
string = [NSString stringWithFormat:@"%d",i];
elementArray =[Obj.obj valueForKey:string];
starthour = [[elementArray objectAtIndex:1]integerValue];
startmin =([[elementArray objectAtIndex:1]floatValue]-[[elementArray objectAtIndex:1]integerValue])*100;
endhour = [[elementArray objectAtIndex:2]integerValue];
endmin =([[elementArray objectAtIndex:2]floatValue]-[[elementArray objectAtIndex:2]integerValue])*100;
NSNumber *day = [NSNumber numberWithInteger:[string integerValue]];
NSNumber *starthour1 = [NSNumber numberWithInteger:(int)starthour];
NSNumber *startmin1 = [NSNumber numberWithInteger:(int)startmin];
NSNumber *endhour1 = [NSNumber numberWithInteger:(int)endhour];
NSNumber *endmin1 = [NSNumber numberWithInteger:(int)endmin];
[temp addObject:day];
[temp addObject:starthour1];
[temp addObject:startmin1];
[temp addObject:endhour1];
[temp addObject:endmin1];
[results addObject:temp];
}
这使我可以访问每条记录。我的思维过程是说,我可以通过遍历每条记录并将所需的数组填充为“ i”作为一天,然后在每条记录上运行一个嵌套循环并提取小时和分钟来解决此问题?
答案 0 :(得分:0)
您没有说出您的问题到底是什么,也就是说,您开始使用解决方案,然后停止并转向SO,为什么?
让我们看看我们是否可以帮助您提供一些伪代码。
您有一个数组字典。键代表日期数字,每个数组的第一个元素也代表数字。在第一个数组元素之后,元素成对出现,每个元素都是一个字符串,其中包含小时和分钟的文本表示形式,并用句号分隔。您需要一个数组,其中每个元素都有5个成员:天数,开始小时,开始分钟,结束小时,结束分钟,全部为数字。
处理此问题的基本算法是两个嵌套循环。您需要在某个地方存储结果:
results <- new empty array
现在您需要遍历字典:
for every key in sourceDictionary
elementArray = sourceDictionary[key]
[这是您编写的循环,只不过您生成了键并在它们从此处从字典中提取键的同时进行了查找。如果您希望按特定顺序处理密钥,则可以按该顺序生成密钥,或对它们进行提取和排序,将对其进行处理。]
现在您最好进行一些数据验证,elementArray中至少应包含日期和一对字符串,对此进行测试:
check elementArray count > 1 and odd else handle error
现在您需要处理每对字符串,首先访问它们:
cursor <- 1 // element 0 is the repeated day number
while cursor < elementArray count do
startTime <- elementArray[cursor]
endTime <- elementArray[cursor+1]
cursor <- cursor + 2 // ready for next iteration
因此,现在您有了日期编号key
,开始和结束时间为字符串startTime
和endTime
。现在,您需要在句号处分割时间字符串,并返回一个子字符串数组:
startParts <- startTime split at "."
更多支票,您得到了两个部分吗?
check startParts count is 2 else handle error
现在将每个子字符串转换为整数:
startHours <- parse startParts[0] as integer
startMins <- parse startParts[1] as integer
并继续检查:
check startHours in [0, 23] and startMins in [0, 59] else handle error
您现在已拥有所需的五个值,并将它们添加到results
数组中:
results <- results append array of (key, startHours, startMins, endHours, endMins)
现在只需将其转换为Objective-C并填写详细信息即可。第一个循环可以是for in
,第二个循环可以是for(init; test; increment)
。要拆分字符串,请查看NSString
的方法,这很合适。要进行解析,有多个选项,包括NSString
方法。
HTH
答案 1 :(得分:0)
我将其与所需的输出配合使用,但是代码看起来很凌乱,可以变得优雅。
` NSMutableArray *elementArray =[[NSMutableArray alloc ]init];
NSMutableArray *results =[[NSMutableArray alloc ]init];
NSInteger starthour = 0 ;
NSInteger startmin = 0 ;
NSInteger endhour = 0 ;
NSInteger endmin = 0 ;
NSNumber *starthour1 ;
NSNumber *startmin1 ;
NSNumber *endhour1 ;
NSNumber *endmin1 ;
NSMutableArray *temp = [[NSMutableArray alloc]init];
NSString *string;
int i = 0;
for ( i = 0; i <=6; i ++){
string = [NSString stringWithFormat:@"%d",i];
elementArray =[Obj.obj valueForKey:string];
starthour = [[elementArray objectAtIndex:1]integerValue];
startmin =([[elementArray objectAtIndex:1]floatValue]-[[elementArray objectAtIndex:1]integerValue])*100;
endhour = [[elementArray objectAtIndex:2]integerValue];
endmin =([[elementArray objectAtIndex:2]floatValue]-[[elementArray objectAtIndex:2]integerValue])*100;
NSNumber *day = [NSNumber numberWithInteger:[string integerValue]];
starthour1 = [NSNumber numberWithInteger:(int)starthour];
startmin1 = [NSNumber numberWithInteger:(int)startmin];
endhour1 = [NSNumber numberWithInteger:(int)endhour];
endmin1 = [NSNumber numberWithInteger:(int)endmin];
[temp addObject:day];
[temp addObject:starthour1];
[temp addObject:startmin1];
[temp addObject:endhour1];
[temp addObject:endmin1];
[results addObject:[temp copy]];
[temp removeAllObjects];
starthour = [[elementArray objectAtIndex:3]integerValue];
startmin =([[elementArray objectAtIndex:3]floatValue]-[[elementArray objectAtIndex:3]integerValue])*100;
endhour = [[elementArray objectAtIndex:4]integerValue];
endmin =([[elementArray objectAtIndex:4]floatValue]-[[elementArray objectAtIndex:4]integerValue])*100;
starthour1 = [NSNumber numberWithInteger:(int)starthour];
startmin1 = [NSNumber numberWithInteger:(int)startmin];
endhour1 = [NSNumber numberWithInteger:(int)endhour];
endmin1 = [NSNumber numberWithInteger:(int)endmin];
[temp addObject:day];
[temp addObject:starthour1];
[temp addObject:startmin1];
[temp addObject:endhour1];
[temp addObject:endmin1];
[results addObject:[temp copy]];
[temp removeAllObjects];
starthour = [[elementArray objectAtIndex:5]integerValue];
startmin =([[elementArray objectAtIndex:5]floatValue]-[[elementArray objectAtIndex:5]integerValue])*100;
endhour = [[elementArray objectAtIndex:6]integerValue];
endmin =([[elementArray objectAtIndex:6]floatValue]-[[elementArray objectAtIndex:6]integerValue])*100;
starthour1 = [NSNumber numberWithInteger:(int)starthour];
startmin1 = [NSNumber numberWithInteger:(int)startmin];
endhour1 = [NSNumber numberWithInteger:(int)endhour];
endmin1 = [NSNumber numberWithInteger:(int)endmin];
[temp addObject:day];
[temp addObject:starthour1];
[temp addObject:startmin1];
[temp addObject:endhour1];
[temp addObject:endmin1];
[results addObject:[temp copy]];
[temp removeAllObjects];
`
输出是我想要的,如下所示。
( ( 0, 7 0, 8, 59 ), ( 0, 15 0, 16 59 ), ( 0, 19 0, 20 59 ), ( 1, 7 0, 8, 59 ), ( 1, 15 0, 16 59 ), ( 1, 22 0, 23 59 ), ( 2, 7 0, 8, 59 ), ( 2, 15 0, 16 59 ), ( 2, 19 0, 20 59 ), ( 3, 7 0, 8, 59 ), ( 3, 15 0, 16 59 ), ( 3, 22 0, 23 59 ), ( 4, 7 0, 8, 59 ), ( 4, 15 0, 16 59 ), ( 4, 19 0, 20 59 ), ( 5, 7 0, 8, 59 ), ( 5, 15 0, 16 59 ), ( 5, 22 0, 23 59 ), ( 6, 7 0, 8, 59 ), ( 6, 15 0, 16 59 ), ( 6, 19 0, 20 59 ) )