0

I have a problem when trying to retrieve values from an array. I want to get all the values from this array in order to display them in a IUTableView.

Should I use a double for function ?

{
1 =     {
    dishId = 1;
    dishName = "Tomato Salades";
    dishPrice = 13;
    dishTypeId = 1;
    dishTypeName = Starter;
};
2 =     {
    dishId = 2;
    dishName = "Leeks Salades";
    dishPrice = 12;
    dishTypeId = 1;
    dishTypeName = Starter;
};
3 =     {
    dishId = 3;
    dishName = Fries;
    dishPrice = 14;
    dishTypeId = 2;
    dishTypeName = "Main Course";
};
4 =     {
    dishId = 4;
    dishName = Beef;
    dishPrice = 15;
    dishTypeId = 2;
    dishTypeName = "Main Course";
};
7 =     {
    dishId = 7;
    dishName = "Cheese Cake";
    dishPrice = 8;
    dishTypeId = 3;
    dishTypeName = Dessert;
};
menuCountry = France;
menuDescription = "un menu pas comme les autres pour une region pas comme les autres";
menuId = 1;
menuName = "Autour de l\\Alsace";
menuState = 1;
}

This is my Python code which creates a Dictionary in a Array :

def getDishOfTheWeek():
menuArray = []
menuDic = Ddict(dict)
    for menu in Menus.select().where(state=True):

        menuDic['menuId']=menu.id
        menuDic['menuName']=menu.name
        menuDic['menuCountry']=menu.country.name
        menuDic['menuDescription']=menu.description
        menuDic['menuState']=menu.state
        for d in  DishMenuRels.select().where(menu = menu.id).join(Dishes).join(DishTypes).order_by((DishTypes,'name')):
            menuDic[str(d.dish.id)] = {}
            menuDic[str(d.dish.id)]['dishTypeName'] = d.dish.dishType.name
            menuDic[str(d.dish.id)]['dishTypeId'] = d.dish.dishType.id
            menuDic[str(d.dish.id)]['dishId'] = d.dish.id
            menuDic[str(d.dish.id)]['dishName'] = d.dish.name
            menuDic[str(d.dish.id)]['dishPrice'] = d.dish.price
menuArray.append(menuDic)
return json.dumps(menuArray)

This is my Objectiv-C code in order to get the DataJson and put it in an Array:

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {

switch (streamEvent) {

    case NSStreamEventOpenCompleted:
        [[ConnectionSingleton getInstance] setConnectionMade:YES];
        NSLog(@"Stream opened");
        break;

    case NSStreamEventHasBytesAvailable:
        if (theStream == inputStream) {

            uint8_t buffer[1024];
            int len;

            while ([inputStream hasBytesAvailable]) {

                len = [inputStream read:buffer maxLength:sizeof(buffer)];

                if (len > 0) {

                    NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                    output = [output substringToIndex:[output length] - 2];

                    if (nil != output) {
                        NSError* error;     // Obligatoir pour le JSON

                        menu = [NSJSONSerialization JSONObjectWithData:[output dataUsingEncoding:NSUTF8StringEncoding] options:
                                  NSJSONReadingMutableContainers error:&error];   // Put Json in the Array


                        [[self tableView] reloadData];  // Reload Array to populate it
                    }
                }
            }
        }
        break;      

    case NSStreamEventErrorOccurred:
        NSLog(@"Can not connect to the host!");
        break;

    case NSStreamEventEndEncountered:
        break;

    default:
        NSLog(@"Unknown event");
}

}
3
  • Is it really an objecive-c array ? And what do you want to do with these values then ? Commented May 8, 2012 at 18:41
  • insert them in different label and cell row And yes it's and objectiv-c array Commented May 8, 2012 at 18:55
  • Is there a reason you don't just use fast enumeration? Commented May 8, 2012 at 19:37

1 Answer 1

1

I think this should do the trick

NSArray *firstArray = [NSArray arrayWithObjects:[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil], [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil],[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil],[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil],[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil],@"String",@"String",@"String",@"String",@"String",nil];

NSInteger count_i = firstArray.count;
NSInteger count_j;
id val;
for (NSInteger i = 0; i < count_i; i++) {
    if ([[firstArray objectAtIndex:i] isKindOfClass:[NSArray class]]) {
        count_j = [[firstArray objectAtIndex:i] count];
        for (NSUInteger j = 0; j < count_j; j++) {
            val = [[firstArray objectAtIndex:i] objectAtIndex:j]; 
            NSLog(@"%@",val);
        }
    }else{
        val = [firstArray objectAtIndex:i];
        NSLog(@"%@",val);
    }
}

But your values are of different types, so you have to deal with them in different ways depending of the value of j.

Sign up to request clarification or add additional context in comments.

6 Comments

How can I get the second array ?
it's doesnt work if i keep the if statement. If I remove it, i got and error in the second for loops : Signal SIGABRT
The last answer is normal because at the end of your array, objects are not array anymore so you can't call objectAtIndex on these object. I just tried my code and it works fine. I edited my answer with an example array. You can try it.
Yes thanks for update, your code's working well, but not with mine ... The function enter in else loop and that display the array I've copy/paste in my question
Ok, maybe it would be good to add the code where you set up your Array, because yours look more like a NSDictionnary with key/value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.