我有一个接收phonenumber并根据条件返回BOOL的方法,如果该phonenumber是否在NSMutableDictionary中。
这是字典
的结构键值
手机1234567890workphone 2345678910
homephone 4252433718
如何检查字典中是否有该字符串?
我尝试过doContain,但即使数字在字典中,也总是返回NO。
答案 0 :(得分:2)
这应该这样做。
- (BOOL) hasNumber:(NSString *) phoneNumber
{
return [[phoneNumbers allValues] containsObject:phoneNumber];
}
答案 1 :(得分:1)
NSNumber *object = [NSNumber numberWithInt:1234567890];
if ([dictionary allKeysForObject:object] count] > 0) {
NSLog(@"The number is in dictionary.");
} else {
NSLog(@"The number is not there in dictionary.");
}
答案 2 :(得分:0)
检查一下..我不确定这是否是你想要的
NSDictionary *dictionary = @{@"workhome":@(123123),@"homephone":@(456456)};
if (dictionary[@"workhome"] != nil)
{
//the key "workhome" has something
}
if ([[dictionary allValues] containsObject:@(123123)])
{
//the user has this number
NSArray *key = [dictionary allKeysForObject:@(123123)];
if (key.count > 0)
{
//now you have all keys for this object.
}
}