I am checking if my one of the array consist the Nsobject
which i have created custom way. 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.
我正在检查我的一个数组是否包含Nsobject,我已经创建了自定义方式。我正在使用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是否包含特定的对象?
Following is my hash and isEqual Method which i have overriden and also shwoing my property types in nsobejct. @interface InterestEntity : JSONModel
以下是我的哈希和isEqual方法,我已经覆盖并且还在nsobejct中破坏了我的属性类型。 @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;
}
1 个解决方案
#1
7
the problem is in implementing isEqual
method in handling nullable objects
问题是在处理可空对象时实现isEqual方法
i.e. 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;
}
#1
7
the problem is in implementing isEqual
method in handling nullable objects
问题是在处理可空对象时实现isEqual方法
i.e. 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;
}