0

如果该电话号码是否在 NSMutableDictionary 中,我有一个方法可以接收电话号码并根据条件返回 BOOL。

这是字典的结构

核心价值

手机1234567890

工作电话 2345678910

家庭电话 4252433718

如何检查该电话号码是否在字典中?

我尝试了doesContain,但即使数字在字典中,它也总是返回NO。

4

3 回答 3

2

这应该这样做。

- (BOOL) hasNumber:(NSString *) phoneNumber
{ 
   return [[phoneNumbers allValues] containsObject:phoneNumber];
}
于 2013-09-13T19:03:27.793 回答
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.");
}
于 2013-09-13T18:40:17.897 回答
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.
    }
}
于 2013-09-13T19:02:11.967 回答