【发布时间】:2013-01-23 13:07:50
【问题描述】:
编辑:大量编辑,很抱歉造成混乱!
我目前正在开发一个应用程序,其中我使用两种不同的方法从 JSON WebService 获取 2 个数组。
在第一个数组中,我得到本地库存。在第二个数组中,我得到了供应商库存。
虽然我认为图片会更好地解释:
因此,Web 服务获取了许多商品。数组 1 包含来自本地库存的一些商品,数组 2 包含来自我们供应商的商品,并添加了描述等信息。我想合并两个数组,用供应商库存数组 2 中的项目描述更新我们本地 stock-array1 中的现有项目。
这是我想要实现的一个示例。所有产品都由唯一标识符属性(示例中未提及)标识,并且我已覆盖 isEqual 和 hash 来弥补这一点。如果一个产品有一个相似的 ID,它被认为是相同的。
**Product**
@property id *internalID;
@property id *externalID ;
@property id *localStock;
@property id *supplierStock;
@property id *image;
@property id *productInfo;
现在Webservice 1返回三个产品:
产品 1
*internalID = ABCDEF
*externalID = (null)
*localStock = 15
*supplierStock = (null)
*image = (null)
*productInfo = (null);
产品 2
*internalID = GHIJK
*externalID = (null)
*localStock = 13
*supplierStock = (null)
*image = niceImage.png
*productInfo = @"This Product is Awesome!";
产品 3
*internalID = LMNOP
*externalID = (null)
*localStock = 7
*supplierStock = (null)
*image = (null)
*productInfo = (null);
Webservice 2 返回四个产品,其中两个也在数组 1 中:
产品 1
*internalID = (null)
*externalID = 123456
*localStock = (null)
*supplierStock = 12
*image = external_product1image.jpg
*productInfo = @"This product is also Awesome and in both local stock and supplier stock!";
产品 4
*internalID = (null)
*externalID = 23456
*localStock = (null)
*supplierStock = 11
*image = niceImage.png
*productInfo = @"This Product is Awesome and only available from our supplier!";
产品 3
*internalID = (null)
*externalID = 78901
*localStock = (null)
*supplierStock = 7
*image = external)supplierimage.jpg
*productInfo = @"This product is also Awesome and in both local stock and supplier stock!";
那么mergedArray 应该是这样的:
产品 1
*internalID = ABCDEF
*externalID = 123456
*localStock = 15
*supplierStock = 12
*image = external_product1image.jpg
*productInfo = @"This product is also Awesome and in both local stock and supplier stock!";
//所以 Array 1 中的 Product 1 将其属性与 Array 2 中的 Product 1 合并
产品 2
*internalID = GHIJK
*externalID = (null)
*localStock = 13
*supplierStock = (null)
*image = niceImage.png
*productInfo = @"This Product is Awesome!";
产品 3
*internalID = LMNOP
*externalID = 78901
*localStock = 7
*supplierStock = 7
*image = external)supplierimage.jpg
*productInfo = @"This product is also Awesome and in both local stock and supplier stock!";
产品 4
*internalID = (null)
*externalID = 23456
*localStock = 13
*supplierStock = 11
*image = niceImage.png
*productInfo = @"This Product is Awesome and only available from our supplier!";
这是我使用的代码,但它似乎有时会以某种方式出现故障:
- (void) compareArrays:(id)sender metBreedte:(NSString *)breedte metHoogte:(NSString *)hoogte metDiameter:(NSString *)diameter
{
NSMutableSet *getBandenSet = [NSMutableSet setWithArray:getBandenArray]; NSUInteger i = 0;
while (i < [getBandenInfo1Array count]) {
id getBandenInfo1Object = [getBandenInfo1Array objectAtIndex:i];
if ([getBandenSet containsObject:getBandenInfo1Object])
{
BWBand *getBandenBand = [getBandenSet member:getBandenInfo1Object];
BWBand *getBandenInfo1Band = getBandenInfo1Object;
// Do stuff to the Banden (sync)
getBandenBand.alternatievePrijs = getBandenInfo1Band.alternatievePrijs;
getBandenBand.itemName = getBandenInfo1Band.itemName;
getBandenBand.supplierStock = getBandenInfo1Band.supplierStock;
getBandenBand.grossPrice = getBandenInfo1Band.grossPrice;
getBandenBand.eancode = getBandenInfo1Band.eancode;
getBandenBand.EMarked = getBandenInfo1Band.EMarked;
getBandenBand.garagePrijs = getBandenInfo1Band.garagePrijs;
getBandenBand.loadIndex = getBandenInfo1Band.loadIndex;
getBandenBand.brand = getBandenInfo1Band.brand;
getBandenBand.custPrice = getBandenInfo1Band.custPrice;
getBandenBand.netPrice = getBandenInfo1Band.netPrice;
getBandenBand.tyreLabel = getBandenInfo1Band.tyreLabel;
getBandenBand.TyreLabelFuel = getBandenInfo1Band.TyreLabelFuel;
getBandenBand.TyreLabelNoise = getBandenInfo1Band.TyreLabelNoise;
getBandenBand.TyreLabelNoiseLevel = getBandenInfo1Band.TyreLabelNoiseLevel;
getBandenBand.TyreLabelWet = getBandenInfo1Band.TyreLabelWet;
getBandenBand.foto = getBandenInfo1Band.foto;
NSMutableString *string1 = [NSMutableString stringWithString: getBandenBand.colorStock];
NSString *newString = [string1 substringToIndex:[string1 length]-2];
NSString *colorStock = [NSString stringWithFormat:@"%@-%@",newString,getBandenInfo1Band.supplierStock];
getBandenBand.colorStock = colorStock;
[getBandenSet removeObject:getBandenInfo1Object];
[getBandenInfo1Array removeObjectIdenticalTo:getBandenInfo1Object];
} else
{i++;}
mergedBandenArray = [NSMutableArray arrayWithArray:[getBandenArray arrayByAddingObjectsFromArray:getBandenInfo1Array]];
[mergedBandenArray sortUsingDescriptors:
[NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@"colorStock" ascending:NO], [NSSortDescriptor sortDescriptorWithKey:@"brand" ascending:YES], nil]];}
所有这些都可以使用 NSPredicates 完成吗?如果有,怎么做?
提前致谢!
【问题讨论】:
-
您的 TableViewController 是在您的 while 循环完成后分配的,所以这不应该是您的问题。
-
仍在检查我的代码。如果有人知道使用 NSPredicates 进行这种比较的更优雅的解决方案,我会全神贯注:)。
-
您是否为您的对象正确设置了哈希和相等操作,以便两个对象比较相等,即使它们是您要合并的不同内部数据?
-
要使表格视图正确刷新,只需在合并完成后发出刷新。或者在你准备好之前根本不显示表格。
-
是的,抱歉,忘记提了。我正在使用 AFNetworking 异步填充两个数组,然后在 AFHTTPClient 的完成块中运行上述代码。如果只有一个唯一标识符相同,我已经覆盖了哈希和 isEqualto 以使它们“相等”。
标签: objective-c merge comparison nsarray nspredicate