Swift operator `subscript` []

后端 未结 6 694
野性不改
野性不改 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条回答
  •  旧时难觅i
    2020-12-31 01:53

    A workaround is to leverage multiple parameters in subscript

    So instead of data[1][2], you can say data[1, 2]. This will be handy in some cases

    struct Container {
      subscript(a: Int, b: Int) -> String {
        print(a)
        print(b)
        return "possible"
      }
    }
    
    let data = Container()
    data[1, 2]
    

提交回复
热议问题