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`
        }
    }
}๐ paiza.io
let bucketList = BucketList(items: [
    "travel to Italy", 
    "have children", 
    "watch super bowl"
])
print(bucketList[0])    // travel to Italy
print(bucketList[4])    // your list is overLast updated
Was this helpful?