Is there any difference between subscript and a function in swift? Can someone explain me with a little example?
if you mean subscripts for custom classes, then no. looks like they are just syntactic sugar for computed properties
class IHaveASubscript<T> {
private var array: Array<T>
init() {
array = []
}
subscript (index: Int) -> T {
get {
return array[index]
}
set(newValue) {
array[index] = newValue
}
}
func elementAtIndex(index: Int) -> T {
return array[index]
}
func setElementAtIndex(index: Int, element: T) {
array[index] = element
}
}