Has Key (associative array)

From SwiftAPI

Jump to: navigation, search

Contents

C++

#include <string>
#include <map>
using namespace std;
 
 
map<string, int> a;
a["test"] = 4;
a["foo"] = 2;
bool b;
 
b = a.count("test") == 1;
// b == true
 
b = a.count("bar") == 1;
// b == false

C#

MSDN

System.Collections.Generic.IDictionary<TKey, TValue> : public bool ContainsKey(TKey key)
using System.Collections.Generic;
 
Dictionary<string, int> a = new Dictionary<string, int>() { { "test", 4 }, { "foo", 2 } };
bool b;
 
b = a.ContainsKey("test");
// b == true
 
b = a.ContainsKey("bar");
// b == false

Java

J2SE 5

java.util.Map<K,V> : boolean containsKey(Object key)
import java.util.Map;
import java.util.HashMap;
 
Map<String, Integer> a = new HashMap<String, Integer>();
a.put("test", 4);
a.put("foo", 2);
boolean b;
 
b = a.containsKey("test");
// b == true
 
b = a.containsKey("bar");
// b == false

Note: Do not confuse the above code with

b = a.get("test") != null;

If get() returns null, it could mean either that the key does not exist, or (in the Map implementations that allow null values) that it does exist, and the value is null. containsKey() will return true in the latter case. Always use containsKey() to test if a key exists.

JavaScript

Mozilla MSDN

var a = { test: 4, foo: 2};
var b;
 
b = 'test' in a;
// b == true
 
b = 'bar' in a;
// b == false

Objective-C

developer.apple.com

NSDictionary *a = @{@"test": @4, @"foo": @2};
BOOL b;
 
b = a[@"test"] != nil;
// b == YES
 
b = a[@"bar"] != nil;
// b == NO

or: developer.apple.com

NSDictionary *a = @{@"test": @4, @"foo": @2};
BOOL b;
 
b = [a objectForKey:@"test"] != nil;
// b == YES
 
b = [a objectForKey:@"bar"] != nil;
// b == NO

Perl

perldoc

exists EXPR
%a = ('test' => 4, 'foo' => 2);
 
$b = exists($a{'test'});
# $b == 1
 
$b = exists($a{'bar'});
# $b == ""

Note: Do not confuse the above code with

$b = defined($a{'test'});

If defined() returns false, it could mean either that the key does not exist, or that it does exist, and the value is undef. exists() will return true in the latter case. Always use exists() to test if a key exists. Use defined() if you want to make sure that the value, if it exists, is not undef.

PHP

php.net

bool array_key_exists ( mixed $key, array $search )
$a = array('test' => 4, 'foo' => 2);
 
$b = array_key_exists($a, 'test');
// $b === true
 
$b = array_key_exists($a, 'bar');
// $b === false

Note: Do not confuse the above code with

$b = isset($a['test']);

If isset() returns false, it could mean either that the key does not exist, or that it does exist, and the value is NULL. array_key_exists() will return true in the latter case. Always use array_key_exists() to test if a key exists. Use isset() if you want to make sure that the value, if it exists, is not NULL.

Python

a = {'test': 4, 'foo': 2}
 
b = 'test' in a
# b == true
 
b = 'bar' in a
# b == false

Ruby

Ruby-Doc.org

hsh.has_key?(key) => true or false
a = {"test" => 4, "foo" => 2}
 
b = a.has_key?("test")
# b == true
 
b = a.has_key?("bar")
# b == false

Swift

let a = ["test": 4, "foo": 2]
 
var b = a["test"] != nil
// b == true
 
b = a["bar"] != nil
// b == false

or: developer.apple.com

NSMutableDictionary *a = [NSMutableDictionary dictionaryWithObjectsAndKeys:@4, @"test",
                                                                           @2, @"foo", nil];
BOOL b;
 
b = [a objectForKey:@"test"] != nil;
// b == YES
 
b = [a objectForKey:@"bar"] != nil;
// b == NO
Personal tools