解析具有所需格式的 NSMutableDictionary

parsing NSMutableDictionary with required format

我有一个 objective 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' 是一天,然后 运行 在每条记录上嵌套循环并提取小时和分钟?

你没有说出你的 问题 实际上是什么,即你从一个解决方案开始然后停下来转向 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

我得到了所需的输出,但代码看起来很乱,可以变得优雅。

` 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 ) )