I define a function that outputs an instance Any. If it is NSArrayor CollectionType, it prints how many elements it has and no more than 10 of its elements:
static func prettyPrint(any: Any) -> String {
switch any {
case is NSArray:
let array = any as! NSArray
var result: String = "\(array.count) items ["
for i in 0 ..< array.count {
if (i > 0) {
result += ", "
}
result += "\(array[i])"
if (i > 10) {
result += ", ..."
break;
}
}
result += "]"
return result
default:
assertionFailure("No pretty print defined for \(any.dynamicType)")
return ""
}
}
I want to add a case clause for anyone CollectionType, but I cannot, because it is a type that includes generics. Compiler message: Protocol 'CollectionType' can only be used as a generic constraint because it has Self or associated type requirements
I just need to iterate and property countto create a print string, I don’t need the type of elements contained in the collection.
How can i check CollectionType<?>?
source
share