我正在学习 Swift 以及 Bloc.io Swiftris教程。
我已经为游戏板创建了一个 Array2D 类
// Generic arrays in Swift are actually of type struct, not class but we need a class in this case since class objects are
// passed by reference whereas structures are passed by value (copied).
// Our game logic will require a single copy of this data structure to persist across the entire game.
// Notice that in the class' declaration we provide a typed parameter: <T>.
// This allows our array to store any type of data and therefore remain a general-purpose tool.
class Array2D<T> {
let columns : Int
let rows : Int
// an actual Swift array; it will be the underlying data structure which maintains references to our objects.
// ? in Swift symbolizes an optional value. An optional value is just that, optional.
// nil locations found on our game board will represent empty spots where no block is present.
var array: Array<T?>
init(colums: Int, rows: Int) {
self.columns = columns // !! Assigning a property to itself.
self.rows = rows
// we instantiate our internal array structure with a size of rows * columns.
// This guarantees that Array2D can store as many objects as our game board requires, 200 in our case.
array = Array<T?>(count:rows * columns, repeatedValue: nil)
}
}
然而,Xcode 显示了一个 Assigning a property to itself.
这一行的警告:
self.columns = columns
我对这个警告有点困惑,这难道不是我想要做的吗?警告是什么意思?
最佳答案
您在构造函数定义中输入错误。
构造函数中的参数名称是colums
。
将其更改为列或将有问题的行更改为
self.columns = colums
它应该可以工作
关于ios - 在类 Initializer 中分配成员时在 Xcode 中向自身分配属性警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26626966/