0

resently i met a problem that is when i use the method 'containObject:XX'. object 'XX' is existed in the array,and all properties of the two objects have the same values,though they have different address. i have caught the data using charles,and confirmed what i said just now. but the method 'containObject:XX' all the same, it makes me puzzled. anybody knows why? thanks.

NSArray *tempCouponArr = [NSArray arrayWithArray:_chosenCouponIndexPathArr];
        for (CouponInfoModel *cim in tempCouponArr) {
            if(![couponArr containsObject:cim])
            {
                [_chosenCouponIndexPathArr removeObject:cim];
                _couponValue -= cim.price;
            }
        } 

when i set a breakpoint i found that current object in couponArr is all the same with current cim without address. but it enter the condition statement that is not what i want.

Leo
  • 1
  • Does your `CouponInfoModel` class properly implement the `isEqual:` and `hash` methods? – rmaddy Mar 22 '16 at 02:10
  • no i don't do that . but as i know,i don't need to do that,when i use containObject,containObject use isEqual,and i think it compares two objects,if the properties of two objects are all the same without address,it will return a YES.isn't it? – Leo Mar 22 '16 at 02:20
  • Your comment makes no sense. You say you don't need to implement `isEqual:` but then you say you do. See the duplicate. – rmaddy Mar 22 '16 at 02:24
  • You definitely need to implement isEqual if you want to use contains. Otherwise it uses the memory locations by default. – drekka Mar 22 '16 at 02:26
  • finally, i overwrite the method isEqual: ,if i dont do this ,it uses the memory locations by default. – Leo Mar 22 '16 at 08:39

0 Answers0