๐ 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` } } }
let bucketList = BucketList(items: [ "travel to Italy", "have children", "watch super bowl" ]) print(bucketList[0]) // travel to Italy print(bucketList[4]) // your list is over
InfoQ โฉ Swift 5.2 Brings callAsFunction, Subscript with Default Arguments, and More
Donny Wals โฉ Adding default values to subscript arguments in Swift 5.2
Last updated 3 years ago