我正在学习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
我对警告感到有点困惑,这不就是我打算做的吗?警告意味着什么?
答案 0 :(得分:7)
你在构造函数定义中输错了。
构造函数中参数的名称为colums
。
将其更改为列或将违规行更改为
self.columns = colums
它应该有效
答案 1 :(得分:2)
你的init声明中有一个拼写错误:
init(colums: Int, rows: Int)
^
你写了colums
而不是columns
答案 2 :(得分:-2)
查看所有代码中单词的正确书写....在任何应用程序中
在所有类、方法等中!不要说Tnank你!