Search code examples
xamarin.iosnssetenumerate

How does Enumerate work in MonoTouch?


In MonoTouch I need to process each object in an NSSet. My attempt, using Enumerate, is as follows:

public override void ReturnResults ( BarcodePickerController picker, NSSet results )
{
    var n = results.Count;  // Debugging - value is 3
    results.Enumerate( delegate( NSObject obj, ref bool stop ) 
    {
        var foundCode = ( obj as BarcodeResult ); // Executed only once, not 3 times
        if ( foundCode != null )
        {
            controller.BarcodeScannedResult (foundCode);
        }
    });
// Etc
}

Although the method is invoked with three objects in results, only one object is processed in the delegate. I would have expected the delegate to be executed three times, but I must have the wrong idea of how it works.

Unable to find any documentation or examples. Any suggestion much appreciated.


Solution

  • You have to set the ref parameter to false. This instructs the handler to continue enumerating:

    if ( foundCode != null )
    {
        controller.BarcodeScannedResult (foundCode);
        stop = false; // inside the null check
    }
    

    Here is the ObjC equivalent from Apple documentation.