I’m new in iPhone, I want to add elements to NSMutableArray with each element’s name
I created a MutableArray for keys , then other array for elements that I get them from object called Pages.
I wrote the following code
NSMutableArray *myArray;
NSMutableArray *arrayKey = [[NSMutableArray alloc] initWithObjects:@"b_pag_id", @"b_pag_bo_id", @"b_pag_num", @"b_pag_note", @"b_page_mark", @"b_page_stop", @"b_pag_user_id", nil];
for (int x=0; x<[pages count]; x++) {
Pages *myPages = (Pages *)[self.pages objectAtIndex:x];
NSString *b_pag_id2 = [NSString stringWithFormat:@"%d",myPages.b_pag_id];
NSString *b_pag_bo_id2 = [NSString stringWithFormat:@"%d",myPages.b_pag_bo_id];
NSString *b_pag_num2 = [NSString stringWithFormat:@"%d",myPages.b_pag_num];
NSString *b_pag_note2 = myPages.b_pag_note;
NSString *b_page_mark2 = [NSString stringWithFormat:@"%d",myPages.b_page_mark];
NSString *b_page_stop2 = [NSString stringWithFormat:@"%d",myPages.b_page_stop];
NSString *b_pag_user_id2 = [NSString stringWithFormat:@"%d",myPages.b_pag_user_id];
NSMutableArray *arrayValue = [[NSMutableArray alloc] initWithObjects:b_pag_id2, b_pag_bo_id2, b_pag_num2, b_pag_note2, b_page_mark2, b_page_stop2, b_pag_user_id2, nil];
NSDictionary *theReqDictionary = [NSDictionary dictionaryWithObjects:arrayValue forKeys:arrayKey];
myArray = [NSMutableArray arrayWithObjects:theReqDictionary,nil];
}
NSLog(@"array size: %d", [myArray count]);
I want to add every element to its key for example
element (b_pag_id2) its key (b_pag_id) ..etc
is this right ?? or how to do this ??
consider that NSLog(@”array size: %d”, [myArray count]); gives me 1 and the size of my elements is 14
Before the loop you need to initialize the aray
Inside the loop replace following:
with
The problem is that you are creating a new array with 1 dictionary in every loop iteration. Instead you need to initialize the array and add values one by one.