[英]NsArray ContainsObject is not working with Custom NsObject Class
I am checking if my one of the array consist the Nsobject
which i have created custom way. 我正在检查我的数组之一是否包含我创建自定义方式的
Nsobject
。 i am using ContainsObject
method.my problem is that contains object not working ever.even though i have same object in array its not returning the true value. 我正在使用
ContainsObject
。我的问题是包含对象无法正常工作。即使我在数组中有相同的对象也没有返回真实值。
if([self.arrSelectedInterest containsObject:interest_ent])
i am also attaching the screen shot of my debug points which showing the value of the nsarray and comparing object and in that i found that interest_ent
is a same object that contains in self.arrSelectedInterest
and still it always return false. 我还附加了调试点的屏幕截图,其中显示了nsarray的值并比较了对象,并且发现我的
interest_ent
是self.arrSelectedInterest
中包含的同一对象,但始终返回false。
anyone have any idea how to check if my nsarray of custom nsobject contains specific Object? 有谁知道如何检查自定义nsobject的nsarray是否包含特定的Object?
Following is my hash and isEqual Method which i have overriden and also shwoing my property types in nsobejct. 以下是我的哈希和isEqual方法,我已重写它们并在nsobejct中显示了我的属性类型。 @interface InterestEntity : JSONModel
@interface InterestEntity:JSONModel
@property (strong, nonatomic) NSString* InterestId;
@property (strong, nonatomic) NSString* Name;
@property (strong, nonatomic) NSString<Optional>* Code;
@property (strong, nonatomic) NSString<Optional>* Description;
@property (strong, nonatomic) NSArray<Optional>* Hashtags;
- (NSUInteger)hash {
NSUInteger result = 1;
NSUInteger prime = 31;
result = prime * result + [self.InterestId hash];
result = prime * result + [self.Name hash];
result = prime * result + [self.Code hash];
result = prime * result + [self.Description hash];
result = prime * result + [self.Hashtags hash];
return result;
}
- (BOOL)isEqual:(id)object {
BOOL result = NO;
if ([object isKindOfClass:[self class]]) {
result = [[self InterestId] isEqualToString:[object InterestId]] &&
[[self Name] isEqualToString:[object Name]] &&
[[self Code] isEqualToString:[object Code]] &&[[self Description] isEqualToString:[object Description]] && [[self Hashtags] isEqual:[object Hashtags]];
}
return result;
}
the problem is in implementing isEqual
method in handling nullable objects 问题是在处理可空对象中实现
isEqual
方法
ie if two strings is null isEqualToString:
will return false 即,如果两个字符串为null,则
isEqualToString:
将返回false
Here is a proper implementation to handle all cases 这是处理所有情况的正确实现
- (BOOL)isEqual:(id)object {
if (object == self)
return YES;
if (!object || ![object isKindOfClass:[self class]])
return NO;
// not nullable fields
if (![self.interestId isEqualToString:object.interestId])
return NO;
if (![self.name isEqualToString:object.name])
return NO;
if (![self.code isEqualToString:object.code])
return NO;
// nullable fields (2 if statements for more clean code)
if (self.description != null && ![self.description isEqualToString:object.description])
return NO;
if (object.description != null && ![object.description isEqualToString:self.description])
return NO;
if (self.hashtags != null && ![self.hashtags isEqualToString:object.hashtags])
return NO;
if (object.hashtags != null && ![object.hashtags isEqualToString:self.hashtags])
return NO;
return YES;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.