Both Objective-C and .NET offers a wide range of collections and maps (NSArray, NSDictionary, etc. for Objective-C and List, Dictionary, etc. for .NET). The Monobjc bridge provides an easy mean to access both Objective-C collections and maps in a .NET way. Here are the mapping offered :

  • The class NSArray can be used as an immutable IList<Id>
  • The class NSMutableArray can be used as a mutable IList<Id>
  • The class NSDictionary can be used as an immutable IDictionary<Id, Id>
  • The class NSMutableDictionary can be used as a mutable IDictionary<Id, Id>
  • The class NSSet can be used as an immutable ICollection<Id>
  • The class NSMutableSet can be used as a mutable ICollection<Id>
  • The class NSEnumerator can be used as an IEnumerator<Id>

The following syntaxes show some examples of how to deal with Objective-C collections and maps in a .NET application.

Iteration is really easy by using the foreach construct:

NSArray voices = NSSpeechSynthesizer.AvailableVoices;
foreach (NSString identifier in voices.GetEnumerator<NSString>())
{
    Console.WriteLine("Voice Identifier " + identifier);
}

Enumeration is also very easy:

NSDirectoryEnumerator enumerator = NSFileManager.DefaultManager.EnumeratorAtPath((NSString) ".");
while (enumerator.MoveNext())
{
    NSString file = enumerator.Current.CastTo<NSString>();
    Console.WriteLine("File " + file);
}

Access to collection is straightforward with the indexed notation:

uint count = groups.Count;
for (int index = 0; index < count; index++)
{
    ABRecord record = groups[index].CastTo<ABRecord>();
    Console.WriteLine("  Group {0}: {1}", index, record.UniqueId);
}

Access to map is straightforward with the indexed notation:

NSArray voices = NSSpeechSynthesizer.AvailableVoices;
foreach (NSString identifier in voices.GetEnumerator<NSString>())
{
    Console.WriteLine("Voice Identifier " + identifier);

    NSDictionary attributes = NSSpeechSynthesizer.AttributesForVoice(identifier);
    NSString name = attributes[NSSpeechSynthesizer.NSVoiceName].CastTo<NSString>();
    Console.WriteLine("Voice Name " + name);
}

Use of collection search or iterations and lambdas are done this way:

NSArray voices = NSSpeechSynthesizer.AvailableVoices;
voices.ForEach(identifier => Console.WriteLine("Voice Identifier " + identifier));