ios - 在类 Initializer 中分配成员时在 Xcode 中向自身分配属性警告

标签 ios xcode swift

我正在学习 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/

相关文章:

ios - 在 Swift 3.0 的 Xcode 8 中使用 dd-MM-yyyy 将字符串转换为日期得到 nil

swift - AudioKit:在 Swift 框架中嵌入 AudioKit

ios - 相当于 OpenGL 混合的 Metal

ios - 通过 ViewController 传递数据

ios - 存折权利?

ios - 在启动屏幕 Storyboard 中按比例重新缩放 UIImageView

objective-c - UITextField 中的缩进

ios - 如何在现有的ios项目中使用cocoapods

objective-c - 从 OS X 命令行编译和链接 Swift 和 Objective C 代码

ios - 您可以在共享的全局类中跟踪用户的位置吗?