Ios 在类初始值设定项中分配成员时,在Xcode中将属性分配给自身时发出警告
我正在学习快速跟随教程 我已经为游戏板创建了一个Array2D类Ios 在类初始值设定项中分配成员时,在Xcode中将属性分配给自身时发出警告,ios,xcode,swift,Ios,Xcode,Swift,我正在学习快速跟随教程 我已经为游戏板创建了一个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 wil
// 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)
}
}
这个警告让我有点困惑,这不正是我想要做的吗?警告是什么意思?您的初始声明中有一个输入错误:
init(colums: Int, rows: Int)
^
您编写了
columns
而不是columns
您在构造函数定义中输入了一个错误
构造函数中参数的名称是colums
将其更改为列或将有问题的行更改为
self.columns = colums
而且它应该可以工作查看所有代码中单词的正确书写……在任何应用程序中
在所有类、方法等中!不要说谢谢你 两个答案几乎相同,但@Rajeev Bhatia的速度更快。无论如何,非常感谢!有点尴尬。谢谢@麦克斯,放松点,几秒钟前我刚遇到过:D
self.columns = colums