« Format button for Fil… | Home | Read and write notes … »

Embracing for each loops

In the recent version of MBS Xojo Plugins, we embrace the for-each loops in Xojo. We got quite a few classes to implement the Iterator and Iterable interfaces:

Each of them implements the interfaces, so you can easily loop over a set of entries easily. For example IterateElementsByTagName loops over all elements in a XML tree and stops by the ones with matching tag names:

Dim doc As New XMLDocumentMBS("<doc><test id=""1"">abc</test><test id=""2"">def</test></doc>")

For Each e As XMLElementMBS In doc.IterateElementsByTagName("test")
MessageBox e.toString
Next

The main goal here is first convenience as you don't need to prepare an array of elements, check for matching the criteria and then loop over them. Doing all this work in fine tuned code inside the plugin is certainly faster than doing it yourself in Xojo.

Same for this example looping over fields in a PDF document:

Dim d As New DynaPDFMBS

For Each Field As DynaPDFFieldExMBS In d.IterateFields

Dim Name As String = Field.FieldName

Break
Next

Please try the new for each loops with various functions here:

  • In DynaPDFMBS class
    • method IterateAnnotations as DynaPDFIteratorMBS
    • method IterateBookmarks as DynaPDFIteratorMBS
    • method IterateCMaps as DynaPDFIteratorMBS
    • method IterateColorSpaces as DynaPDFIteratorMBS
    • method IterateEmbeddedFiles(Decompress as boolean = false) as DynaPDFIteratorMBS
    • method IterateErrLogMessages as DynaPDFIteratorMBS
    • method IterateFields as DynaPDFIteratorMBS
    • method IterateFonts as DynaPDFIteratorMBS
    • method IterateImages(ImageFlags as Integer = 0) as DynaPDFIteratorMBS
    • method IterateLayerConfigurations as DynaPDFIteratorMBS
    • method IterateNamedDestinations as DynaPDFIteratorMBS
    • method IterateOCGs as DynaPDFIteratorMBS
    • method IterateOutputIntents as DynaPDFIteratorMBS
    • method IteratePageAnnotations as DynaPDFIteratorMBS
    • method IteratePageLabels as DynaPDFIteratorMBS
    • method IterateSysFonts as DynaPDFIteratorMBS
    • method IterateXFAStreams as DynaPDFIteratorMBS
  • In JSONMBS class
    • method Iterate as JSONIteratorMBS
    • method IterateEntries as JSONIteratorMBS
    • method IterateValues as JSONIteratorMBS
  • In PCRE2CodeMBS class
    • method Matches(Text as String, StartOffsetCharacters as Integer = 0, MatchContext as PCRE2MatchContextMBS = nil) as PCRE2IteratorMBS
  • In XMLDocumentMBS class
    • method createNodeIterator(root as XMLNodeMBS = nil, whatToShow as Integer = &hFFFF, entityReferenceExpansion as boolean = false, filter as XMLNodeFilterMBS = nil) as XMLNodeIteratorMBS

If you have an idea for new iterator, please let us know.

18 11 23 - 12:39