문제가 발생하여이를 극복 할 수있는 방법을 찾을 수 없습니다. 기본적으로 일부 값으로 변경 가능한 사전을 만들어야합니다. 모든 값은 동적이며 웹 서비스 또는 다른 변수에서 가져옵니다.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
}
는 또한 내 사전에 다음과 같은 트리를 추가 할 수 있지만이 작업을 수행하는 방법을 알아낼 수 없습니다.
나는 2NSArray
artID
및
qty
에서 필요한 항목이 있지만 내가
dict
에 추가 할 수 있도록 하단 파트를 생성하는 방법을 모르겠어요.
Carts {
Cart {
ArticleID : 22
Quantity : 1
}
Cart {
ArticleID : 45
Quantity : 3
}
...
}
나는 [dict setObject:carts forKey:@"Cart"]
로 추가하지만 내가 당신을 제시하는 양식에 내 dictionary
를 만들 것입니다 같은 방식으로 값을 추가하는 방법을 모르겠어요.
또한 숫자 또는 Carts
이 유동적임을 잊지 마십시오. 내가 Product.count
에서 얻을 것이다.
미리 감사드립니다. 배열 artID
및 qty
모두이
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"];
}
같은 시도 할 수있는 사전을 만들에 대해 동일한 인덱스에 값이있는 경우
귀하의 답변은 부분적으로 정확했지만 해결책을 찾는데 도움이되었습니다. –