编辑:大量编辑,抱歉导致混淆!
我正在开发一个应用程序,其中我使用两种不同的方法从JSON WebService获得2个数组。
在第一个数组中,我获得了本地股票。在第二个数组中,我得到供应商库存。
虽然我认为图像可以更好地解释:
因此,webservices获得了许多项目。数组1包含来自本地库存的一些项目,而数组2包含来自供应商的项目,以及添加的信息,如描述等。我想合并两个数组,将本地stock-array1中的现有项目更新为供应商库存数组2中项目的描述。
这是我想要实现的一个例子。所有产品都由唯一的标识符属性标识(在示例中未提及),并且我重写了isEqual和hash来补偿它。如果产品具有simliar 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!";
//因此,阵列1中的产品1将其属性与阵列2中的产品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完成所有这些吗?如果是这样,怎么样?
提前致谢!
答案 0 :(得分:4)
以下算法不要求您为isEqual
类覆盖hash
和Product
:
uniqueItemID
键按升序对两个数组进行排序。如果两个数组都已排序,则可以轻松找到匹配和不匹配的产品,只需一个循环并行遍历两个数组。
以下代码有希望证明这一想法:
NSSortDescriptor *sortByID = [NSSortDescriptor sortDescriptorWithKey:@"uniqueItemID" ascending:YES];
NSArray *sorted1 = [array1 sortedArrayUsingDescriptors:@[sortByID]];
NSArray *sorted2 = [array2 sortedArrayUsingDescriptors:@[sortByID]];
NSUInteger i1 = 0, i2 = 0;
NSMutableArray *combined = [NSMutableArray array];
while (i1 < [sorted1 count] && i2 < [sorted2 count]) {
Product *p1 = sorted1[i1];
Product *p2 = sorted2[i2];
switch ([p1.uniqueItemID compare:p2.uniqueItemID]) {
case NSOrderedAscending:
// p1 is in first array, but not in second:
[combined addObject:p1];
i1++;
break;
case NSOrderedDescending:
// p2 is in second array, but not in first:
[combined addObject:p2];
i2++;
break;
case NSOrderedSame:
// p1, p2 have same ID:
... update p1 with values from p2 ...
[combined addObject:p1];
i1++, i2++;
break;
}
}
// Add remaining products from first array:
while (i1 < [sorted1 count]) {
[combined addObject:sorted1[i1]];
i1++;
}
// Add remaining products from second array:
while (i2 < [sorted2 count]) {
[combined addObject:sorted2[i2]];
i2++;
}
答案 1 :(得分:3)
1)。合并两个数组:
NSMutableSet *set = [NSMutableSet setWithArray:array1]; [set addObjectsFromArray:array2]; NSArray *array = [set allObjects];
2)。要在NSPredicates的帮助下删除重复项:
1. Filtering Fun with Predicates
3)。合并&amp;删除重复项:
NSArray *array1, *array2; MSMutableArray *result = [array1 mutableCopy]; for (id object in array2) { [result removeObject:object]; // remove duplicates if it's already there. [result addObject:object]; }