As I already discussed on a previous post (see https://nsleofont.github.io/blog/2018/01/19/swift-isempty ) if we need to check if one of our collections is empty, Swift provides a very easy and convenient way to do it using the isEmpty property.

However there’s no nice way to check if a collection is NOT empty and we end up writing ugly code like:

// Having the negation exclamation mark on first position
// is counterintuitive.

if !collection.isEmpty  (...)  

// Gets worst if you have to force unwrap an optional...

if !collection!.isEmpty (...) // Ouch!

How handy it would be to be able to do something like: collection.isNotEmpty wouldn’t it?

This small extension will do exactly that, wrapping the ugliness behind the scenes and allowing your code to be shiny and easier to read.

extension Array {
    
    public var isNotEmpty: Bool {
        
        return !self.isEmpty
    }
}

From now you’ll be able to write beautiful code :P:

if collection.isNotEmpty  (...) // Much better!!!

Happy coding!!! :)