Q:

Swift: Array contains(AnyObject) error Cannot convert type of ... throw -> Bool

Why I use Array.contains(AnyObject) to checking whether an structure object exists in this Array. It makes error : "Cannot convert type of ... throw -> Bool"
struct DecorationPatternsData {
    let patternImageName: String
    init(patternImageName: String) {
        self.patternImageName = patternImageName
    }
}
var decorationPatterns : [DecorationPatternsData] = [DecorationPatternsData(patternImageName: "decoration1.gif"), DecorationPatternsData(patternImageName: "decoration1.gif"), DecorationPatternsData(patternImageName: "decoration1.gif")]

var pickedDecorationPattern : DecorationPatternsData? = nil

...

if (pickedDecorationPattern != nil) {
     if (decorationPatterns.contains(pickedDecorationPattern)) {  
         // Error: Cannot convert type of ... throw -> Bool
     }
}

A:

That's because your DecorationPatternsData doesn't conform to Equatable which is a requirement for contains(_:) to work.

Solution 1:
extension DecorationPatternsData: Equatable { }

func ==(lhs: DecorationPatternsData, rhs: DecorationPatternsData) -> Bool {
    return lhs.patternImageName == rhs.patternImageName
}

Now DecorationPatternsData conforms to Equatable so you can use:
if let pickedDecorationPattern = pickedDecorationPattern {
    if decorationPatterns.contains(pickedDecorationPattern) {
    // Your code
    }
}

Solution 2:
if decorationPatterns.contains({ $0 == pickedDecorationPattern }) {
    // Your code
}

Here you're using a closure to compare the elements, returning a bool for contains(_:)

