Web services IOS 5:如何在包含2';相同';对象,但指针不同

Web services IOS 5:如何在包含2';相同';对象,但指针不同,web-services,ios5,hash,nsmutablearray,equality,Web Services,Ios5,Hash,Nsmutablearray,Equality,我是StackOverflow的新手,如果我忘了一些,请提前道歉 我的问题中的关键因素。这是一个很长的,所以请容忍我 迭代JSON Web服务结果: MyCust: SideShowBobsEarWarmers Hash: **2082010619** AmountInShoppingCart: 4 **<MyCust: 0x81cee50>**", " MyCust: SolidSnakesCardBoardBox Hash: **4174540990** Am

我是StackOverflow的新手,如果我忘了一些,请提前道歉 我的问题中的关键因素。这是一个很长的,所以请容忍我

迭代JSON Web服务结果:

MyCust: SideShowBobsEarWarmers
 Hash: **2082010619**
 AmountInShoppingCart: 4
 **<MyCust: 0x81cee50>**",
    "
 MyCust: SolidSnakesCardBoardBox
 Hash: **4174540990**
 AmountInShoppingCart: 4
 **<MyCust: 0x8190d10>**" 
对于IOS5 iPad应用程序,我通过以下方式从Web服务收集JSON信息:

NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *json = 
[NSJSONSerialization JSONObjectWithData:data options:kNilOptions      
error:&error];
我将遍历返回的
NSDictionary
,并将返回的对象及其属性存储为自定义类

自定义类包含多个属性,包括
amountingshoppingcart
-值

我使用这个自定义类来表示我们在商店中销售的商品,因此由所有返回的对象组成一个
NSMutableArray
,并显示给用户。 如果
amountingshoppingcart值>0
,则对象将放入
ShoppingCart
NSMutableArray
(这是一个单例)

在ShoppingCart中检查重复项:

MyCust: SideShowBobsEarWarmers
 Hash: **2082010619**
 AmountInShoppingCart: 4
 **<MyCust: 0x81cee50>**",
    "
 MyCust: SolidSnakesCardBoardBox
 Hash: **4174540990**
 AmountInShoppingCart: 4
 **<MyCust: 0x8190d10>**" 
为了确保
ShoppingCart
不包含任何重复条目,我在
ShoppingCartController
中有一个类似的函数(也是从SO借用的):

同步两个阵列:

MyCust: SideShowBobsEarWarmers
 Hash: **2082010619**
 AmountInShoppingCart: 4
 **<MyCust: 0x81cee50>**",
    "
 MyCust: SolidSnakesCardBoardBox
 Hash: **4174540990**
 AmountInShoppingCart: 4
 **<MyCust: 0x8190d10>**" 
人们可能会从JSON Web服务请求相同的结果,因为它使用MyCust的大小、高度和直径返回一组遵循这些大小的对象

因此,我使用以下函数检查两个NSMutableArray并将其与新结果“同步”:

In: RESULTLISTCONTROLLER.M

- (void) syncWithCart
{
    ShoppingCart *sc = [ShoppingCart sharedCart];
    [sc checkForDuplicates];
    NSMutableSet *listSet = [NSMutableSet setWithArray:resultList];
    NSUInteger i = 0;

    while (i < [sc.ShoppingCartArray count]) {
        id ShopObject = [sc.ShoppingCartArray objectAtIndex:i];
        if ([listSet containsObject:ShopObject])
        {
            MyCust *listCust = [listSet member:ShopObject];
            MyCust *shopCust = ShopObject;
            listCust.AmountInShoppingCart = shopCust.AmountInShoppingCart;
            [listSet removeObject:ShopObject];
        } else {i++;}}}

现在的问题是,当用户检查相同大小的两次时,应用程序似乎没有考虑到新的结果相同。

当我对ShoppingCartArray执行NSLog时,如下所示: (原始项目名称模糊)

第一次搜索:

MyCust: SideShowBobsEarWarmers
 Hash: **2082010619**
 AmountInShoppingCart: 4
 **<MyCust: 0x81cee50>**",
    "
 MyCust: SolidSnakesCardBoardBox
 Hash: **4174540990**
 AmountInShoppingCart: 4
 **<MyCust: 0x8190d10>**" 
MyCust:SideShowBobsEarWarmers
散列:**2082010619**
购物车数量:4
****",
"
MyCust:SolidSnakesCardBoardBox
散列:*4174540990**
购物车数量:4
****" 
第二次搜索: ResultList不保留MyCust的AmountInShoppingCart值,因此我从ResultList再次将它们添加到ShoppingCart中

 MyCust: SideShowBobsEarWarmers
 Hash: **2082010619**
 AmountInShoppingCart: 4
 **<MyCust: 0x81cee50>**",

 MyCust: SolidSnakesCardBoardBox
 Hash: **4174540990**
 AmountInShoppingCart: 4
 **<MyCust: 0x8190d10>**" 
    "
// Different Pointers, although they should be considered Equal.

 MyCust: SideShowBobsEarWarmers
 Hash: **2082010619**
 AantalInWinkelWagen: 3
 **<MyCust: 0x74bc570>**",      
    "
 MyCust: SolidSnakesCardBoardBox
 Hash: **4174540990**
 AantalInWinkelWagen: 2
 **<MyCust: 0x74bc7b0>**"
MyCust:SideShowBobsEarWarmers
散列:**2082010619**
购物车数量:4
****",
MyCust:SolidSnakesCardBoardBox
散列:*4174540990**
购物车数量:4
****" 
"
//不同的指针,尽管它们应该被认为是相等的。
MyCust:SideShowBobsEarWarmers
散列:**2082010619**
安塔林温克尔瓦根:3
****",      
"
MyCust:SolidSnakesCardBoardBox
散列:*4174540990**
安塔林温克尔瓦根:2
****"
因此,不知何故,编译器在不同的内存地址/指针中分配了这些对象,然后就我所知,中断了
isEqual:
函数


我的直觉告诉我这与
NSCopy(able)有关
,但我不太确定我将如何实现它。如果有人得到了一些关于购物车系统的好建议,那也总是很受欢迎的。我有一半的想法将所有这些转换为核心数据,但希望在这样做之前将核心功能准备就绪。

在isEqual:method中,行“If(externalItemId!”[otherCust externalItemId])返回“否”是错误的。
我认为如果(![externalItemId:isEqual:[otherCust externalItemId]])返回NO;

我将
isEqual
方法更改为:
-(BOOL)isEqual:(id)rhs{//重写isEqual以确保从Web服务返回的MyCust与shoppingcart或list中已存在的MyCust相同BOOL ret=NO;如果([rhs isKindOfClass:[MyCust class]]//不要使用[self class]{ret=[self externalItemId]=[rhs externalItemId];}return ret;}-(NSUInteger)hash{return[[self externalItemId]hash];}
哇,谢谢!这马上就解决了,已经让我发疯好几天了。