|
WebObjects 5.2 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.webobjects.foundation.NSDictionary
The NSDictionary class declares an API for objects that manage immutable associations of keys and values. This class, or its subclass NSMutableDictionary, is used when a convenient and efficient way is needed to retrieve data associated with an arbitrary key. (For convenience, we use the term dictionary to refer to any instance of one of these classes without specifying its exact class membership.)
A key-value pair within a dictionary is called an entry. Each entry
consists of one object that represents the key, and a second object which
is that key's value. Within a dictionary, the keys are unique. That is, no
two keys in a single dictionary are equal (as determined by equals
).
An instance of NSDictionary is an immutable dictionary: you establish its entries when it is created, and cannot modify them afterwards. An instance of NSMutableDictionary is a mutable dictionary: you can add or delete entries at any time, and the object automatically allocates memory as needed.
Methods that add entries to dictionaries, whether during construction (for
all dictionaries) or modification (for mutable dictionaries), add each
value object to the dictionary directly. This means that you must ensure that
the keys do not change. If you expect your keys to change for any reason, you should
make copies of the keys and add the copies to the dictionary or otherwise
guarantee that the keys will not change with respect to either the equals
or
hashCode
methods. java.lang.String
objects are immutable
and make good keys.
The NSDictionary methods that provide the basis for all NSDictionary's other the NSDictionary methods that provide the basis for all NSDictionary's other methods; that is, all other methods are implemented in terms of these four. If you create a subclass of NSDictionary, you need only ensure that these base methods work properly. Having done so, you can be sure that all your subclass's inherited methods operate properly.
Method | Description |
count | Returns the number of entries in the dictionary. |
objectForKey | Returns the value associated with a given key. |
keysNoCopy | Returns a natural language array containing the keys in the dictionary. |
objectsNoCopy | Returns a natural language array containing the objects in the dictionary. |
The other methods declared here operate by invoking one or more of these primitives. The non-primitive methods provide convenient ways of accessing multiple entries at once.
Map
,
HashMap
,
Hashtable
,
count()
,
objectForKey(java.lang.Object)
, Serialized FormInner classes inherited from class com.webobjects.foundation.NSCoding |
NSCoding.Support |
Inner classes inherited from class com.webobjects.foundation.NSKeyValueCoding |
NSKeyValueCoding.DefaultImplementation, NSKeyValueCoding.ErrorHandling, NSKeyValueCoding.Null, NSKeyValueCoding.UnknownKeyException, NSKeyValueCoding.Utility, NSKeyValueCoding.ValueAccessor |
Inner classes inherited from class com.webobjects.foundation.NSKeyValueCodingAdditions |
NSKeyValueCodingAdditions.DefaultImplementation, NSKeyValueCodingAdditions.Utility |
Field Summary | |
static NSDictionary |
EmptyDictionary
A NSDictionary instance containing no entries which may be shared. |
Fields inherited from interface com.webobjects.foundation.NSKeyValueCoding |
NullValue |
Fields inherited from interface com.webobjects.foundation.NSKeyValueCodingAdditions |
KeyPathSeparator |
Constructor Summary | |
NSDictionary()
Creates an empty dictionary. |
|
NSDictionary(Dictionary dictionary,
boolean ignoreNull)
Creates a dictionary containing the keys and values found in dictionary . |
|
NSDictionary(NSArray objects,
NSArray keys)
Creates an NSDictionary with entries from the contents of the keys
and objects NSArrays. |
|
NSDictionary(NSDictionary otherDictionary)
Creates a dictionary containing the keys and values found in otherDictionary . |
|
NSDictionary(Object[] objects,
Object[] keys)
Creates an NSDictionary with entries from the contents of the keys
and objects arrays. |
|
NSDictionary(Object object,
Object key)
Creates a dictionary containing a single object object for
a single key key . |
Method Summary | |
NSArray |
allKeys()
The order of the elements in the returned array is not defined. |
NSArray |
allKeysForObject(Object object)
Finds all occurrences of the value object in the dictionary
and returns a new array with the corresponding keys. |
NSArray |
allValues()
The order of the values in the array is not defined. |
Class |
classForCoder()
Conformance to NSCoding. |
Object |
clone()
Returns a copy (an NSDictionary object) of this dictionary. |
int |
count()
This method returns a count of the number of key-value pairs. |
static Object |
decodeObject(NSCoder coder)
Creates an NSDictionary from the data in coder . |
void |
encodeWithCoder(NSCoder coder)
Encodes the receiver using coder . |
boolean |
equals(Object object)
Compares this dictionary to object . |
int |
hashCode()
|
Hashtable |
hashtable()
|
NSDictionary |
immutableClone()
Since NSDictionaries are immutable, there's no need to make an actual copy. |
boolean |
isEqualToDictionary(NSDictionary otherDictionary)
Provided for backward compatibility. |
Enumeration |
keyEnumerator()
This method provides a java.util.Enumeration which can be used to access
each key in this dictionary. |
protected Object[] |
keysNoCopy()
This method should only be used by subclasses. |
NSMutableDictionary |
mutableClone()
|
Enumeration |
objectEnumerator()
This method provides a java.util.Enumeration which can be used to access
each value in this dictionary. |
Object |
objectForKey(Object key)
This method returns the Object associated with key |
NSArray |
objectsForKeys(NSArray keys,
Object notFoundMarker)
This method takes a subset of the dictionary as specified by the array of keys
and returns an array of the value objects associated with each of the keys. |
protected Object[] |
objectsNoCopy()
This method should only be used by subclasses. |
void |
takeValueForKey(Object value,
String key)
Conformance to NSKeyValueCoding. |
void |
takeValueForKeyPath(Object value,
String keyPath)
Conformance to NSKeyValueCodingAdditions. |
String |
toString()
|
Object |
valueForKey(String key)
Conformance to NSKeyValueCoding. |
Object |
valueForKeyPath(String keyPath)
Conformance to NSKeyValueCodingAdditions. |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final NSDictionary EmptyDictionary
Constructor Detail |
public NSDictionary()
EmptyDictionary
constant should be
used instead.EmptyDictionary
public NSDictionary(Object object, Object key)
object
for
a single key key
.
null
is an illegal value for both object
and key
.Note: NSDictionary assumes that key objects are immutable. If your key objects are mutable, you should make copies of them and add the copies to the dictionary.
object
- object to be stored in the dictionarykey
- key corresponding to the object to be stored in the
dictionarypublic NSDictionary(Object[] objects, Object[] keys)
keys
and objects
arrays.
This method steps through objects
and keys
, creating
entries in the new dictionary as it goes. Each key object and its
corresponding value object is added directly to the dictionary. None of the objects
are permitted to be null
.Note: NSDictionary assumes that key objects are immutable. If your key objects are mutable, you should make copies of them and add the copies to the dictionary.
objects
- array of objects to be stored in the dictionarykeys
- array of keys corresponding to the object to be stored
in the dictionaryjava.lang.IllegalArgumentException
- if the object array and
the key array do not have the same size, or any element is null
public NSDictionary(NSArray objects, NSArray keys)
keys
and objects
NSArrays.
This method steps through objects
and keys
, creating
entries in the new dictionary as it goes. Each key object and its
corresponding value object is added directly to the dictionary.Note: NSDictionary assumes that key objects are immutable. If your key objects are mutable, you should make copies of them and add the copies to the dictionary.
objects
- array of objects to be stored in the dictionarykeys
- array of keys corresponding to the objects to be stored
in the dictionaryjava.lang.IllegalArgumentException
- if the object array
and the key array do not have the same sizepublic NSDictionary(NSDictionary otherDictionary)
otherDictionary
.otherDictionary
- the input dictionary from which the duplicate
dictionary is to be createdpublic NSDictionary(Dictionary dictionary, boolean ignoreNull)
dictionary
. If ignoreNull
is
false
, throws an IllegalArgumentException
if
any key or value in dictionary
is null
.dictionary
- the input dictionary from which the current
dictionary is to be createdignoreNull
- true
if null
is to be allowed in the input fields.java.lang.IllegalArgumentException
- when null
is not
allowed and a null
is encounteredMethod Detail |
public NSArray allKeys()
allValues()
,
allKeysForObject(java.lang.Object)
,
keyEnumerator()
public NSArray allKeysForObject(Object object)
object
in the dictionary
and returns a new array with the corresponding keys.
Each object in the dictionary is sent an equals
message to
determine if it is equalivalent to object
.
If no Object matching object
is found,
this method returns an empty array.object
- the search target in the dictionaryobject
allKeys()
,
keyEnumerator()
public NSArray allValues()
allKeys()
,
objectEnumerator()
public Class classForCoder()
classForCoder
in interface NSCoding
public Object clone()
clone
in class Object
this
mutableClone()
,
immutableClone()
,
Object.clone()
public int count()
public static Object decodeObject(NSCoder coder)
coder
.coder
- the input coderNSCoding
public void encodeWithCoder(NSCoder coder)
NSCoding
coder
. Object type information along with an
object's data is stored.encodeWithCoder
in interface NSCoding
com.webobjects.foundation.NSCoding
coder
- an NSCoder object that will be used to encode object of classes that
implement this interfaceNSCoder
public boolean equals(Object object)
object
.
If object
is an instanceof NSDictionary
and the contents of
object
are equal to these contents, this
method returns true
.
If not, it returns false
.
Two dictionaries have equal contents if they each hold the same
number of entries and, for each key, the corresponding value
objects in each dictionary satisfy the equals
test.
equals
in class Object
object
- the object to compare this dictionary againsttrue
if this dictionary is equivalent to object
, else false
Object.equals(java.lang.Object)
public int hashCode()
hashCode
in class Object
Object.hashCode()
public Hashtable hashtable()
java.util.Hashtable
public NSDictionary immutableClone()
mutableClone()
,
clone()
public boolean isEqualToDictionary(NSDictionary otherDictionary)
equals
instead.otherDictionary
- the dictionary to compare this one totrue
if both the input dictionary and the
this dictionary are equivalent, else false
equals(java.lang.Object)
public Enumeration keyEnumerator()
java.util.Enumeration
which can be used to access
each key in this dictionary.
When this method is used with mutable subclasses of NSDictionary, your code shouldn't modify the entries during enumeration. If you intend to modify the entries, use thejava.util.Enumeration enumerator = myDict.keyEnumerator(); while (enumerator.hasMoreElements()) { Object anObject = enumerator.nextElement(); //code to act on each element }
allKeys
method to create a
"snapshot" of the dictionary's keys. Then use this snapshot to
traverse the entries, modifying them along the way.
Note that the objectEnumerator
method provides a convenient way
to access each value in the dictionary.
allKeys()
,
allKeysForObject(java.lang.Object)
,
objectEnumerator()
protected Object[] keysNoCopy()
allKeys
except the keys might not be copied.objectsNoCopy()
,
allKeys()
public NSMutableDictionary mutableClone()
immutableClone()
,
clone()
public Enumeration objectEnumerator()
java.util.Enumeration
which can be used to access
each value in this dictionary.
java.util.Enumeration enumerator = myDict.objectEnumerator(); while (enumerator.hasMoreElements()) { Object anObject = enumerator.nextElement(); //code to act on each element }
When this method is used with mutable subclasses of NSDictionary,
your code shouldn't modify the entries during enumeration. If
you intend to modify the entries, use the allValues
method to
create a "snapshot" of the dictionary's values. Work from this
snapshot to modify the values.
keyEnumerator()
,
allValues()
public Object objectForKey(Object key)
key
key
- the key to be searched forkey
, or null
if no value is
associated with key
allKeys()
,
allValues()
public NSArray objectsForKeys(NSArray keys, Object notFoundMarker)
keys
and returns an array of the value objects associated with each of the keys.
The objects in the returned array and the keys
array have
a one-for-one correspondence, so that the nth object in the
returned array corresponds to the nth key in keys
. If an
object is not found in this dictionary for a corresponding key,
the notFoundMarker
Object is placed in that key's
position in the resultant array.keys
- a subset of keysnotFoundMarker
- a marker object indicating that the object
corresponding to a key was not foundkeys
protected Object[] objectsNoCopy()
allValues
except the objects might not be copied.keysNoCopy()
,
allValues()
public void takeValueForKey(Object value, String key)
takeValueForKey
in interface NSKeyValueCoding
value
- the input objectkey
- the key corresponding to the objectjava.lang.IllegalStateException
- NSKeyValueCoding.takeValueForKey(java.lang.Object, java.lang.String)
,
NSKeyValueCodingAdditions.takeValueForKeyPath(java.lang.Object, java.lang.String)
public void takeValueForKeyPath(Object value, String keyPath)
takeValueForKeyPath
in interface NSKeyValueCodingAdditions
value
- the input objectkeyPath
- the path of the input objectjava.lang.IllegalStateException
- NSKeyValueCodingAdditions.takeValueForKeyPath(java.lang.Object, java.lang.String)
,
NSKeyValueCoding.takeValueForKey(java.lang.Object, java.lang.String)
public String toString()
toString
in class Object
public Object valueForKey(String key)
objectForKey
.
It provides an entry's value given its key
, or null
if no value is
associated with key
.
Additionally, all dictionaries have the keys "allValues", "allKeys", and "count" defined, which
provide a result identical to the invocation of the method of that name on this dictionary.
If the dictionary contains an actual key which conflicts with these special keys, the real
key trumps the computation. For example, if a dictionary has the key-value pair "count"-"dog"
valueForKey
will return "dog" instead of the result of the count
method.
valueForKey
in interface NSKeyValueCoding
key
- the key to be searched forobjectForKey(java.lang.Object)
,
NSKeyValueCoding.valueForKey(java.lang.String)
,
NSKeyValueCodingAdditions.valueForKeyPath(java.lang.String)
public Object valueForKeyPath(String keyPath)
keyPath
exists as a distinct key in the dictionary, this method returns the corresponding
object in the dictionary by invoking objectForKey
.
Otherwise, it recursively invokes valueForKey
for each segment of the keyPath
on
the result of the previous segment.valueForKeyPath
in interface NSKeyValueCodingAdditions
keyPath
- the key path of the objectkeypath
objectForKey(java.lang.Object)
,
NSKeyValueCodingAdditions.valueForKeyPath(java.lang.String)
,
NSKeyValueCoding.valueForKey(java.lang.String)
|
Last updated Fri Feb 21 13:15:00 PST 2003. | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |