ios - iOS Swift 中的下标选项

标签 ios swift

我有一个教程,我正在从中学习 Swift 和有关下标选项的内容。以下是有关此主题的文档示例的简单代码。

struct Matrix {
    let rows: Int, columns: Int
    var print: [Double]
    init(rows: Int, columns: Int) {
        self.rows = rows
        self.columns = columns
        print = Array(count: rows * columns, repeatedValue: 0.0)
    }
    subscript(row: Int, column: Int) -> Double {
        get {
            return print[(row * columns) + column]
        }
        set {
            print[(row * columns) + column] = newValue
        }
    }
}
var mat = Matrix(rows: 3, columns: 3)

mat[0,0] = 1.0
mat[0,1] = 2.0
mat[1,0] = 3.0
mat[1,1] = 5.0

println("\(mat[0,0])")
println("\(mat[0,1])")
println("\(mat[1,0])")
println("\(mat[1,1])")

任何人都可以解释一下这里发生了什么吗?我无法从这个例子中看出下标选项。可能会有很多像我这样的人。提前致谢。

最佳答案

Subscripts enable you to query instances of a type by writing one or more values in square brackets after the instance name. Their syntax is similar to both instance method syntax and computed property syntax. You write subscript definitions with the subscript keyword, and specify one or more input parameters and a return type, in the same way as instance methods. Unlike instance methods, subscripts can be read-write or read-only. This behavior is communicated by a getter and setter in the same way as for computed properties

我认为苹果 description很好。

如果您喜欢 Ray Wenderlich - 请参阅 here .

关于ios - iOS Swift 中的下标选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33995658/

相关文章:

swift - iOS 13 - WkWebView : audio stops when in background

ios - 使用 Storyboard/ Action

ios - 从被拒绝的证书中清除 XCode 缓存

ios - 在 iPad DetailView 情况下,viewDidUnload 未被调用

ios - 使用 Facebook 电子邮件登录未返回

用于分发应用程序的 iOS Enterprise 选项

swift - 在 OSX 上快速处理同步

objective-c - 如何完美计算 UILabel 所需的高度,其中属性字符串包含 HTML 标记

ios - 不能在不可变值数组上使用可变成员是一个只获取属性 swift 3

ios - FBSDKLoginBehaviorSystemAccount 不工作