Q:

Pretty Test Multiple Variables against Nil value in Swift

Is there any pretty way to test the below? I have multiple parameters which I need to know if any one of them is nil
This is what I am using now, I am sure there is an efficient way to test all and type nil once but not sure how:
if title == nil || name == nil  || height == nil || productName == nil {
            //Do something
        }

I am using ObjectMapper and at they moment, they don't support error handling, hence, my init() throws errors and I need to check if the values from Map are nil or not and through if they are.

A:

I have created a simple extension on CollectionType to check for a collection of Optional value, if at least one element is not nil, if all elements have value or if none have value.
extension CollectionType where Generator.Element == Optional<AnyObject>, Index.Distance == Int  {

    func allNotNil() -> Bool {
       return !allNil()
    }

    func atleastOneNotNil() -> Bool {
        return self.flatMap { $0 }.count > 0
    }

    func allNil() -> Bool {
        return self.flatMap { $0 }.count == 0
    }
}

var title: String? = ""
var name: String? = ""
var height: Float? = 1
var productName: String? = ""

[title, name, height, productName].allNotNil()
[title, name, height, productName].atleastOneNotNil()
[title, name, height, productName].allNil()

In your case, you could use it like this,
 if [title, name, height, productName].atLeastOneNotNil() {

  }

Or, you could discard the extension above and simply use it like this,
 if [title, name, height, productName].flatMap { $0 }.count > 0 {

 }

For Swift 4,
extension Collection where Element == Optional<Any> {

    func allNotNil() -> Bool {
        return !allNil()
    }

    func atleastOneNotNil() -> Bool {
        return self.flatMap { $0 }.count > 0
    }

    func allNil() -> Bool {
        return self.flatMap { $0 }.count == 0
    }
}

Updates for Swift 5, 
Few new functions have been added to CollectionType such as first(where:) and allSatisfy(where:) and it is used here.
extension Collection where Element == Optional {
func allNil() -> Bool {
    return allSatisfy { $0 == nil }
}

func anyNil() -> Bool {
    return first { $0 == nil } != nil
}

func allNotNil() -> Bool {
    return !allNil()
}

}

A:

Here's a short version using a collection literal:
let isAnyNil = ([title, name, height, productName, nil] as [Optional<Any>]).contains { $0 == nil }

It's similar to @GeneratorOfOne's flatMap and count variant. I prefer the simplicity of contains.
If you do this often, I'd go with a free function to avoid the need to specify the type:
func isAnyNil(optionals: Optional<Any> ...) -> Bool {
    return optionals.contains { $0 == nil }
}

isAnyNil(title, name, height, productName)

