如何在iOS中创建2d数组

时间:2014-06-02 10:37:43

标签: ios objective-c

NSString *irResponse=@"%Customer,10^01,10^02,10^03,10^04,10^05,10^06,10^07,10^08,10^09,10^10,10^12,10^13,10^14,10^15,10^16,10^17,10^19,10^20,10^21,10^22,10^23,10^24,10^26,10^27,10^28$26958,Dr.AshokKalwar^97@01@1,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,X^0,^0,^0,$45802,Dr.HarvinderSingh^97@01@1,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,X^0,$26957,Dr.SandeepJain^97@01@1,X^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,$26943,Dr.SurendraBeniw^97@01@1,X^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,$%";

我想拆分这个字符串并将其存储在数组中并在表视图中显示它。

数组格式应该是

Name          code

Dr.Surendr    X^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0,^0

这是我尝试过的代码:

    if ([irResponse hasPrefix:PERCENT] && [irResponse hasSuffix:PERCENT]) {
        irResponse = [irResponse substringWithRange:NSMakeRange(1, irResponse.length - 2)];
    }
    dollerSplit=[[irResponse componentsSeparatedByString:DOLLAR] mutableCopy];

    self.commaSplit=[[NSMutableArray alloc]init];

    for (int  index=0; index<dollerSplit.count-1; index++) {
        [self.commaSplit addObject:[dollerSplit[index] componentsSeparatedByString:COMMA]];
    }

    [headerDataArray addObjectsFromArray:self.commaSplit[0]];

    for (int index=1; index<headerDataArray.count; index++) {
        [DataHeadr addObject:[headerDataArray[index] componentsSeparatedByString:COMMA]];
    }

    for (int index=0; index<DataHeadr.count; index++) {
        [DataValue addObject:[DataHeadr[index][0] componentsSeparatedByString:CAP]];
    }

    for (int index=0; index<DataValue.count; index++) {
        [Array1 addObject:DataValue[index][1]];
    }

    for (int index=1 ; index<self.commaSplit.count; index++) {
        [partyName addObject:[self.commaSplit[index][1] componentsSeparatedByString:CAP]];

    }

    for (int index=0; index<partyName.count; index++) {
        [docter_Name_Array addObjectsFromArray:[partyName[index][0] componentsSeparatedByString:COMMA]];
    }


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


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

    for (int index=0; index<docter_Name_Array.count; index++) {

        for (int i=0 ; i < DataValue.count; i++) {
            [test2 insertObject:[self.commaSplit[index][i] componentsSeparatedByString:CAP] atIndex:index];
        }

        [test1 addObject:test2];

    }

}//end of else

1 个答案:

答案 0 :(得分:1)

NSMutableArray *subarr = [[NSMutableArray alloc] initWithCapacity: 2];

[subarr insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:0];
[subarr insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:1];

这就是你从数组中选择值的方法

NSMutableArray *subdetArray = [subarr objectAtIndex:2];
NSLog(@"data : %@",[subdetArray objectAtIndex:0]);

另一种选择

将值保存在NSDictionary中,并将该字典添加到数组中

NSMutableArray *theArr =  [NSMutableArray array];

    for (int i = 0; i<3; i++) {
       NSMutableDictionary *theDict = [[NSMutableDictionary alloc] init];
       [theDict setObject:name forKey:@"yournemekey"];
       [theDict setObject:age forKey:@"yournemekey"];
       [theArr addObject: theDict];
  }

在检索时,

NSString *name = [[theArr objectAtIndex:indexValue] objectForKey:@"yournemekey"];
NSString *age = [[theArr objectAtIndex:indexValue] objectForKey:@"yournemekey"];