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 に答える