iOS Guides | Samples

MonoTouch.Foundation.NSDictionary Class

Dictionary that provides mapping from keys to values.

See Also: NSDictionary

Syntax

Remarks

This is an immutable dictionary, to create mutable ones, use the NSMutableDictionary type.

There is a convenient constructor that takes at least one key and one value, with optional values that will create the dictionary by pairing each key with a value. If you use .NET objects, the keys and values will first be boxed into NSObjects using NSObject.FromObject.

C# Example

//
// Using C# objects, strings and ints, produces
// a dictionary with 2 NSString keys, "key1" and "key2"
// and two NSNumbers with the values 1 and 2
//
var dict = new NSDictionary ("key1", 1, "key2", 2);

//
// Using NSObjects directly, gives you fine control
//
var key1 = new NSString ("key1");
var value1 = new NSNumber ((byte) 1);
var key2 = new NSString ("key2");
var value2 = new NSNumber ((byte) 2);

var dict2 = new NSDictionary (key1, value1, key2, value2);

Sometimes, you might have already an array of keys and values, so you can use the following factory methods:

C# Example

var keys = new object [] { "key1", "key2" };
var values = new object [] { "value1", "value2" }:

var dict = NSDictionary.FromObjectsAndKeys (values, keys);

The NSDictionary type implements the ICollection, IDictionary and the generic ICollection and IDictionary interfaces, which makes it convenient to use with existing .NET APIs and also provides indexers that take an NSObject, an NSString or a C# string as well as an enumerator.

In some cases, where you might be iterating over a loop, or you have not surfaced a bound type, but you have the handle to the key, you can use the NSDictionary.LowlevelObjectForKey which takes a handle for the key and returns a handle for the returned object.

Related content

Requirements

Namespace: MonoTouch.Foundation
Assembly: monotouch (in monotouch.dll)
Assembly Versions: 0.0.0.0

The members of MonoTouch.Foundation.NSDictionary are listed below.

See Also: NSObject

Public Constructors

Default constructor that initializes a new instance of this class with no parameters.
A constructor that initializes the object from the data stored in the unarchiver object.
Creates a new NSDictionary from an existing NSDictionary.
Constructor to call on derived classes to skip initialization and merely allocate the object.
Creates a new dictionary from the contents of a PropertyList located at a specified URL.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.
Creates a new dictionary from the contents of a PropertyList located in the specified file.
Creates an NSDictionary from a list of NSObject keys and NSObject values.
Creates an NSDictionary from a list of keys and values.

Public Properties

[read-only]
override
ClassHandleIntPtr. The handle for this class.
[read-only]
CountUInt32. Returns the number of keys in the dictionary.
[read-only]
DescriptionInStringsFileFormatString. The contents of the dictionary, formatted in .strings file format.
default property
Item(NSObject)NSObject. Returns the value associated from a key in the dictionary, or null if the key is not found.
default property
Item(NSString)NSObject. Returns the value associated from a key in the dictionary, or null if the key is not found.
default property
Item(String)NSObject. Returns the value associated from a key in the dictionary, or null if the key is not found.
[read-only]
KeysNSObject[]. The set of keys for the NSDictionary.
[read-only]
ObjectEnumeratorNSEnumerator. An NSEnumerator of the values in the NSDictionary.
[read-only]
ValuesNSObject[]. The array of values in the NSDictionary.

Public Methods

ContainsKey(NSObject) : Boolean
Determines whether the specified key exists in the dictionary.
Copy(NSZone) : NSObject
Performs a copy of the underlying Objective-C object.
static
FromDictionary(NSDictionary) : NSDictionary
Factory method to create a new NSDictionary that is a copy of the specified source NSDictionary.
static
FromFile(String) : NSDictionary
Creates a new dictionary from the contents of a PropertyList located in the specified file.
static
FromObjectAndKey(NSObject, NSObject) : NSDictionary
Factory method to create a new NSDictionary initialized with the specified obj (value) and key.
static
FromObjectsAndKeys(NSObject[], NSObject[]) : NSDictionary
Creates a dictionary from a set of values and keys.
static
FromObjectsAndKeys(Object[], Object[]) : NSDictionary
Creates a dictionary from a set of values and keys.
static
FromObjectsAndKeys(NSObject[], NSObject[], Int32) : NSDictionary
Creates a dictionary from a set of values and keys.
static
FromObjectsAndKeys(Object[], Object[], Int32) : NSDictionary
Creates a dictionary from a set of values and keys.
static
FromUrl(NSUrl) : NSDictionary
Creates a new dictionary from the contents of a PropertyList located at a specified URL.
GetEnumerator() : IEnumerator<KeyValuePair<NSObject, NSObject>>
static
GetSharedKeySetForKeys(NSObject[]) : NSObject
Creates a shared key set for the specified keys.
IsEqualToDictionary(NSDictionary) : Boolean
True if the dictionaries have the same number of entries and, for all keys, the values return true for Equals.
KeysForObject(NSObject) : NSObject[]
An array, possibly empty, of the keys whose value is equal to the specified obj.
LowlevelObjectForKey(IntPtr) : IntPtr
Low-level key lookup.
MutableCopy(NSZone) : NSObject
Performs a copy of the underlying Objective-C object.
ObjectForKey(NSObject) : NSObject
The object mapped to the specified key, or null.
ObjectsForKeys(NSArray, NSObject) : NSObject[]
An array of objects for the specified keys. If the key is not found, the marker object is placed in the returned array at the appropriate index.
ToFileAttributes() : NSFileAttributes
TryGetValue(NSObject, out NSObject) : Boolean
WriteToFile(String, Boolean) : Boolean
Attempts to write the NSDictionary to the specified path. If useAuxiliaryFile is true, the file is written atomically. Returns success or failure.
WriteToUrl(NSUrl, Boolean) : Boolean
Attemps to write the NSDictionary to the specified url, and whether to do so atomically. Returns success or failure.

Explicitly Implemented Interface Members

NSDictionary$ICollection<KeyValuePair<NSObject,NSObject>>.Add(KeyValuePair<NSObject,NSObject>)
NSDictionary$IDictionary<NSObject,NSObject>.Add(NSObject,NSObject)
NSDictionary$IDictionary.Add(Object,Object)
NSDictionary$ICollection<KeyValuePair<NSObject,NSObject>>.Clear
NSDictionary$IDictionary.Clear
NSDictionary$ICollection<KeyValuePair<NSObject,NSObject>>.Contains(KeyValuePair<NSObject,NSObject>)
NSDictionary$IDictionary.Contains(Object)
NSDictionary$ICollection.CopyTo(Array,Int32)
NSDictionary$ICollection<KeyValuePair<NSObject,NSObject>>.CopyTo(KeyValuePair<NSObject,NSObject>[],Int32)Creates a copy of this object, allocating memory from the specified zone or from the default zone if the argument is null.
[read-only]
NSDictionary$ICollection<KeyValuePair<NSObject,NSObject>>.CountInt32.
[read-only]
NSDictionary$ICollection.CountInt32.
NSDictionary$IDictionary.GetEnumerator
NSDictionary$IEnumerable.GetEnumerator
[read-only]
NSDictionary$IDictionary.IsFixedSizeBoolean.
[read-only]
NSDictionary$ICollection<KeyValuePair<NSObject,NSObject>>.IsReadOnlyBoolean.
[read-only]
NSDictionary$IDictionary.IsReadOnlyBoolean.
[read-only]
NSDictionary$ICollection.IsSynchronizedBoolean.
NSDictionary$IDictionary.Item(Object)Object.
[read-only]
NSDictionary$IDictionary<NSObject,NSObject>.KeysICollection<NSObject>.
[read-only]
NSDictionary$IDictionary.KeysICollection.
NSDictionary$IDictionary<NSObject,NSObject>.Remove(NSObject)
NSDictionary$ICollection<KeyValuePair<NSObject,NSObject>>.Remove(KeyValuePair<NSObject,NSObject>)
NSDictionary$IDictionary.Remove(Object)
[read-only]
NSDictionary$ICollection.SyncRootObject.
[read-only]
NSDictionary$IDictionary<NSObject,NSObject>.ValuesICollection<NSObject>.
[read-only]
NSDictionary$IDictionary.ValuesICollection.