如何在单个数组的不同索引上获得不同的数组

时间:2012-08-06 09:26:40

标签: objective-c ios xcode nsmutablearray

我有一个名为invoiceInfo1&的数组。我将它传递给另一个名为allInfo的数组。

但是我想要不同的invoiceInfo索引,创建不同的数组&将它传递给allInfo。

我的代码如下:

for (int i=0; i<[userdata count]; i++) {  

    NSLog(@" userdata count :%d",[userdata count]);

    invoiceInfo1 = [NSMutableArray arrayWithObjects:[[userdata objectAtIndex:i]valueForKey:@"fname"],  [[userdata
    objectAtIndex:i]valueForKey:@"name"],

    [[userdata objectAtIndex:i]valueForKey:@"address"],

    [[userdata objectAtIndex:i]valueForKey:@"city"], nil];
    NSLog(@" info1 is:%@",invoiceInfo1); // invoiceInfo get overwrite when loop execute

    NSMutableArray* allInfo = [NSMutableArray arrayWithObjects:headers,invoiceInfo1 ,  nil]; 

    // HERE I Want Generate New Array of different index of invoiceInfo1 & pass it to allInfo

}

1 个答案:

答案 0 :(得分:0)

使用header和info数组创建一个NSObject类:

@interface AllInfo: NSObject
{
    NSMutableArray *header;
    NSMutableArray *info;
}

@property (nonatomic, retain) NSMutableArray *header;
@property (nonatomic, retain) NSMutableArray *info;

@end

@implementation AllInfo

@synthesize header;
@synthesize info;

@end

然后实现此代码:

allInfo = [[NSMutableArray alloc] init];
for (int i=0; i<[userdata count]; i++) {  

    NSLog(@" userdata count :%d",[userdata count]);

    NSMutableArray *invoiceInfo1 = [[NSMutableArray alloc] init]; 
    [invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:@"fname"]];
    [invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:@"name"]];
    [invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:@"address"]];
    [invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:@"city"]];

    NSLog(@" info1 is:%@",invoiceInfo1); // invoiceInfo get overwrite when loop execute
    AllInfo *newInfo = [[AllInfo alloc] init];
    newInfo.header = headers;
    newInfo.info = invoiceInfo1;
    [allInfo addObject:newInfo]; 

    // HERE I Want Generate New Array of different index of invoiceInfo1 & pass it to    allInfo

}

希望这会有所帮助。