在其他 NSArray 的基础上显示来自 NSArray 的值 [英] Showing value from NSArray on the basis of other NSArray
问题描述
(我的英语不好).
我有两个 NSArray.
示例:第一个 NSArray 正在存储
NSArray category= { ID:1,Name:Category1;ID:2, Name: Category2;ID:3, Name:Category3;}
第二个 NSArray 正在存储
NSArray product= {cat_ID:1, Category-Name:Category1, product_ID:1, Name: Banana;
cat_ID:1, Category-Name:Category1, product_ID:2, Name: apple;
cat_ID:1, Category-Name:Category1, product_ID:3, Name: berry;
cat_ID:2, Category-Name:Category2, product_ID:4, Name: cantaloupe;
cat_ID:2, Category-Name:Category2, product_ID:5, Name: elderberry;
cat_ID:2, Category-Name:Category2, product_ID:6, Name: ginger;
cat_ID:2, Category-Name:Category2, product_ID:7, Name: watermelon;
cat_ID:3, Category-Name:Category3, product_ID:8, Name: xigua;
cat_ID:3, Category-Name:Category3, product_ID:9, Name: avocado;
cat_ID:3, Category-Name:Category3, product_ID:10, Name: basil;
cat_ID:3, Category-Name:Category3, product_ID:11, Name: alfalfa;
cat_ID:3, Category-Name:Category3, product_ID:12, Name: blackcurrant;}
我想要的是如果类别名称(我的意思是第一个 NSArray 类别名称)等于第二个 NSArray 的类别名称(我的意思是产品类别名称),然后打印所有产品名称.示例:category.name== Category1;那么结果将是 Product.Name = Banana, Apple, Berry;
All i want is if category name (i mean 1st NSArray category.name) is equals to 2nd NSArray's category name (i mean product.Category-Name) then print all product name. Example: category.name== Category1; then result will be Product.Name = Banana, Apple, Berry;
如何做到这一点?
推荐答案
根据您的类似 json 的表示法,我们将假设{...}"中的键值对是 NSDictionary 对象.你有一个 NSDictionary 对象数组,[{...},{...},...]"
From your json-like notation, we will assume that key value pairs in "{...}" are NSDictionary objects. And you have an array of said NSDictionary objects, "[{...},{...},...]"
要获取给定类别的产品:
To get the products for a given category:
NSString *category = @"Category1";
NSArray *result = [NSArray array];
for (NSDictionary *dic in products) {
if ([[dic objectForKey:@"Category-Name"] isEqualToString: category])
result = [result arrayByAddingObject: [dic objectForKey: @"Name"] ];
}
这篇关于在其他 NSArray 的基础上显示来自 NSArray 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!