2016-08-08 2 views
-1

문제가 발생하여이를 극복 할 수있는 방법을 찾을 수 없습니다. 기본적으로 일부 값으로 변경 가능한 사전을 만들어야합니다. 모든 값은 동적이며 웹 서비스 또는 다른 변수에서 가져옵니다.NSMutableDictionary에 배열 값 추가

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
    [dict setObject:comment.text forKey:@"Comment"]; 
    [dict setObject:name.text forKey:@"Name"]; 
    [dict setObject:[NSString stringWithFormat:@"%d",isPublic] forKey:@"VisibleForAll"]; 
po dict 
{ 
    Comment = "no comment"; 
    Name = "test"; 
    VisibleForAll = 1; 
    -> carts 
} 

는 또한 내 사전에 다음과 같은 트리를 추가 할 수 있지만이 작업을 수행하는 방법을 알아낼 수 없습니다.

나는 2 NSArray artIDqty에서 필요한 항목이 있지만 내가 dict에 추가 할 수 있도록 하단 파트를 생성하는 방법을 모르겠어요.

Carts { 
     Cart { 
      ArticleID : 22 
      Quantity : 1 
      } 
     Cart { 
      ArticleID : 45 
      Quantity : 3 
      } 
     ... 
     } 

나는 [dict setObject:carts forKey:@"Cart"]로 추가하지만 내가 당신을 제시하는 양식에 내 dictionary를 만들 것입니다 같은 방식으로 값을 추가하는 방법을 모르겠어요.

또한 숫자 또는 Carts이 유동적임을 잊지 마십시오. 내가 Product.count에서 얻을 것이다.

미리 감사드립니다. 배열 artIDqty 모두이

NSMutableArray *carts = [[NSMutableArray alloc] init]; 
for(NSInteger i=0; i<products.count; i++) { 
    //If you have custom class `cart` than use that 
    Cart *cart = [[Cart alloc] init]; 
    cart.ArticleID = [[products objectAtIndex:i] valueForKey:@"pid"]; 
    cart.Quantity = [[products objectAtIndex:i] valueForKey:@"qty"]; 

    //If you not have any custom class than use Dictionary 
    NSMutableDictionary *cart = [[NSMutableDictionary alloc] init]; 
    [cart setObject:[[products objectAtIndex:i] valueForKey:@"pid"] forKey:@"ArticleID"]; 
    [cart setObject:[[products objectAtIndex:i] valueForKey:@"pid"] forKey:@"Quantity"]; 
} 

같은 시도 할 수있는 사전을 만들에 대해 동일한 인덱스에 값이있는 경우

답변

2

지금 키

[dict setObject:carts forKey:@"carts"]; 
+0

귀하의 답변은 부분적으로 정확했지만 해결책을 찾는데 도움이되었습니다. –

0
NSArray *pid = [products valueForKey:@"pid" ]; 
    NSArray *qty = [products valueForKey:@"qty"]; 

    NSMutableArray *carts = [[NSMutableArray alloc] initWithCapacity:products.count]; 



    for(NSInteger i=0; i<products.count; i++) { 
     NSMutableDictionary *cart = [[NSMutableDictionary alloc] init]; 
     NSMutableDictionary *cartemp = [[NSMutableDictionary alloc] init]; 

     [cart setObject:[pid objectAtIndex:i] forKey:@"ArticleId"]; 
     [cart setObject:[qty objectAtIndex:i] forKey:@"Quantity"]; 
     [cartemp setObject:cart forKey:@"Cart"]; 
     [carts addObject:cartemp]; 
    } 

    [dict setObject:carts forKey: @"Carts"]; 
Dictionarycarts 배열을 추가
관련 문제