NSImage tinted as easily as a UIImage

I was looking for a simple way of tinting an NSImage the way I’m used to doing with UIImage but it appears that Cocoa is a little behind Cocoa Touch in this regard. I found many different solutions but none of them worked out of the box with Swift 4. This is what I came up with eventually and works as expected.

extension NSImage {
    func tinting(with tintColor: NSColor) -> NSImage {
        guard let cgImage = self.cgImage(forProposedRect: nil, context: nil, hints: nil) else { return self }
        return NSImage(size: size, flipped: false) { bounds in
            guard let context = NSGraphicsContext.current?.cgContext else { return false }
            context.clip(to: bounds, mask: cgImage)
            return true