如何在NSString属性匹配另一组NSString属性的对象中搜索NSArray对象? [英] How do I search an NSArray of objects for an object whose NSString properties match another set of NSString properties?

查看:477
本文介绍了如何在NSString属性匹配另一组NSString属性的对象中搜索NSArray对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里我从核心数据中获取所有对象并存储在NSArray中:

  //设置fetch请求
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@BagItem];

//获取提取的对象并存储在NSArray中
NSArray * fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:& error];

每个对象都有一个title属性,一个size属性和一个color属性。我有一个UIPickerView,从这里我可以选择特定项目的大小和颜色,然后点击应用使用核心数据存储它们。



我想防止重复行。例如:

 皮革大衣,蓝色,X-Large(数量5)

皮革大衣,蓝色,X大($ 1)

我想避免这种情况,具有特定标题,大小和颜色的对象已存在。如果是,我想更新现有行的数量标签。



到目前为止,我已经做了一半。我有一个详细信息视图控制器,它检测到这个魔术,我切换到一个tableView显示对象。但是在tableView我允许客户编辑颜色和大小。这使得客户有机会将对象/项目的属性更新为他们希望的任何颜色和尺寸,即使存在具有相同颜色和尺寸集合的现有相同项目。



我想阻止这种情况。我想要做的是:


  1. 搜索数组,匹配当前的标题,大小和颜色


  2. 更新匹配项目的数量标签

    p>然后删除不再需要的项目的行。*


我只是想解释



所以我需要知道的是如何搜索我的NSArray一个项目,其NSString属性匹配另一组NSStrings。

感谢您的时间。

解决方案

您可以使用NSPredicate:

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@title ==%@ AND size ==%@ AND color ==%@ bagItem.title,bagItem.size,bagItem.color]; 
NSArray * filtered = [arrayOfItems filteredArrayUsingPredicate:predicate];

您可以使用KVC取得总和,例如:

  NSNumber * total = [filtered valueForKeyPath:@@ sum.quantity]; 

然后,您可以创建一个新的BagItem实例,其标题颜色和大小以及新数量值。
因为更新您的模型并根据您的设置重新载入表格。


So here I grab all my objects from core data and store in an NSArray:

// Set up fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"BagItem"];

// Get fetched objects and store in NSArray
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];

Each object has a title property, a size property and a colour property. I have a UIPickerView and from here I can select sizes and colours for a specific item then tap apply to store them using core data.

I'd like to prevent duplicate rows. For example:

Leather Coat, Blue, X-Large (Quantity 5)

Leather Coat, Blue, X-Large (Quantity 1)

I want to avoid this and instead detect that an object with a specific title, size and colour already exists. If it does I'd like to update the quantity label of the existing row.

So far I have half done this. I have a detail view controller that detects this and does the magic before I switch over to a tableView that displays the objects. However while on the tableView I allow customers to edit the colour and size. This gives customers the opportunity to update the object/item's properties to any colour and size they wish even if there is an existing identical item with the same colour and size set.

I am trying to prevent this. What I want to do is:

  1. Search array for item with matching title, size and colour of the current item I am trying to modify.

  2. Update the quantity label of the matching item.

  3. Then remove the row of the item no longer needed.*

I just thought it would be best to explain what I was trying to do.

So what I need to know is how to search my NSArray for an item whose NSString properties match another set of NSStrings.

Thanks for your time.

解决方案

You can use a NSPredicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title == %@ AND size == %@ AND colour == %@", bagItem.title, bagItem.size, bagItem.color];
NSArray *filtered = [arrayOfItems filteredArrayUsingPredicate:predicate];

You can get the sum with KVC, something like:

NSNumber *total = [filtered valueForKeyPath:@"@sum.quantity"];

You could then create a new BagItem instance with the same values for title colour and size and the new quantity value. Than it's up to you to update your model and reload your table as per your setup.

这篇关于如何在NSString属性匹配另一组NSString属性的对象中搜索NSArray对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆