Skip to content

stefanspringer1/LoopsOnOptionals

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LoopsOnOptionals


⚠️ NOTE:

This repository is deprecated; use the following, currently maintained repository instead:

https://github.com/swiftxml/LoopsOnOptionals

Note the reset version number: The last version of this repository corresponds to version 1.0.0 of the new repository.


Mini library to extend optional chains to include for-loops.

Example when using the SwiftXML library:

Suppose an element can have multiple <metadata> children, but for some reason only the first one is relevant, and you want to iterate over its <item> children, but it is also ok if there is no <metadata> at all:

for metaDataItem in element.firstChild("metadata")?.children("item") {
    ...
}

Of course, especially in this simple case you can express the same as follows, without using the LoopsOnOptionals package:

if let metadata = element.firstChild("metadata") {
    for metaDataItem in metadata {
        ...
    }
}

But even more so in more complex situations, the introduction of such a if let (or case let) expression makes the code harder to understand.

About

Mini library to extend optional chains to include for loops

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages