I want use self for having default value, but Xcode cannot find it! how can I solve the issue? I want to have a default value also an option to chose the range in case.
extension Array {
func printItemsIn(range: Range<Int> = self.indices) {
range.forEach { index in
print(self[index])
}
}
}
use case:
[10, 20, 30, 40].printItemsIn(range: 0..<1)
You can't. What you can do is declare your parameter as optional and set its default value to nil.
extension Array {
func printItems(in range: Range<Int>? = nil) {
let range = range ?? indices
range.forEach { index in
print(self[index])
}
}
}
[10, 20, 30, 40].printItems(in: 0..<1) // 10
[10, 20, 30, 40].printItems() // 10, 20, 30, 40
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments