2012-05-08 120 views
0

我試圖從數組中檢索值時遇到問題。 我想從該數組中獲取所有值,以便在IUTableView中顯示它們。如何獲取多維數組的值

我應該使用雙重函數嗎?

{ 
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; 
} 

這是在一個陣列創建一個字典我的Python代碼:

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) 

這是爲了獲得DataJson並把它放在一個陣列我Objectiv-C代碼:

- (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"); 
} 

} 
+0

它真的是一個objecive-c數組嗎?那麼你想用這些值做什麼? –

+0

將它們插入不同的標籤和單元格行 是的它和objectiv -c數組 – socrateisabot

+0

是不是有一個原因,你不只是使用快速枚舉? – Daniel

回答

1

我想這應該做的伎倆

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); 
    } 
} 

但是你的價值觀是不同類型的,所以你必須以不同的方式處理它們,這取決於j的價值。

+0

如何獲得第二個數組? – socrateisabot

+0

對不起,我的壞。我編輯了我的答案 –

+0

如果我保留if語句,它是行不通的。如果我刪除它,我得到了第二個循環的錯誤:信號SIGABRT – socrateisabot