I have a parameter for a method that is of type Range<Int>?
that defaults to nil. I'd like a nice succinct way of saying "Use the given range or use the whole range". Nothing I've done seems to quite work, I feel like this should though...
class func fileNames(entries: Array<DBFILESMetadata>, range: Range<Int>? = nil) -> Array<String> {
return entries[range ?? 0... as Range<Int>].map{ $0.name }.filter{ $0.hasSuffix(".jpg") }
}
...I get the error Cannot convert value of type 'CountablePartialRangeFrom<Int>' to type 'Range<Int>' in coercion
, which I totally get, but then why does this work (via pattern #6 here: https://stackoverflow.com/a/42860580/84783)
let array = ["a", "b", "c"]
let range = 1...
let arraySlice = array[range]
I can just do this in a conditional, but feel like a one liner should work.