编辑:巨大的编辑,很抱歉造成混乱!
我目前正在开发一个应用程序,其中我使用两种不同的方法从 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
返回三个产品:
产品一
*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 中:
产品一
*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
应该是这样的:
产品一
*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 完成吗?如果是这样,怎么做?
提前致谢!