Swift operator `subscript` []

后端 未结 6 712
野性不改
野性不改 2020-12-31 01:01

I am beginner with the Swift having no advance knowledge with operators.

I have the following class

class Container {
   var list: [Any]         


        
6条回答
  •  孤城傲影
    2020-12-31 02:00

    class Container
    {
        var list: [AnyObject] = ["hello" , "world"];
        subscript ( i : Int) -> AnyObject{
            get{
                return list[i]
            }
            set{
                list[i] = newValue
            }
        }
    }
    
    var c : Container = Container()
    println(c[1])
    
    c[1] = "lol"
    
    println(c[1])
    

    For more information about operator : http://nshipster.com/swift-operators/

提交回复
热议问题