Subscripts with Default Arguments
๐ paiza.io
struct BucketList {
var items: [String]
// โญ๏ธ subscript with default value
subscript(index: Int, default: String = "your list is over") -> String {
if index >= 0 && index < items.count {
return items[index]
} else {
return `default`
}
}
}
Last updated
Was this helpful?