I am beginner with the Swift having no advance knowledge with operators.
I have the following class
class Container {
var list: [Any]
class Container {
private var list = [Any]()
subscript(_ index: Int) -> Int? {
get {
guard index < list.count else { return nil }
return list[index]
}
set(newValue){
guard let unwrappedElement = newValue else { return }
list.insert(unwrappedElement, at: index)
}
}
}
Here we are using subscripts with optional type to handle index out of range.