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