【问题标题】:cs193p calculator: why doesn't it let me declare a property? here's the simple codecs193p 计算器:为什么不让我声明一个属性?这是简单的代码
【发布时间】:2016-03-21 19:52:20
【问题描述】:

[cs193p] Swift 计算器:为什么它不让我声明一个属性?这是简单的代码...

我正在使用 swift 1.2

错误是: 从未使用过变量“数字”的初始化;考虑替换为“_”的赋值 或删除它

这是代码

(它不喜欢'let digit'...'var digit'得到同样的错误)

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var display: UILabel!

    @IBAction func appendDigit(sender: UIButton) {
        //initialization of immutable value digit...
        let digit = sender.currentTitle//<-- doesnt like this!!!!!!

    }

}

【问题讨论】:

  • 该消息非常清楚问题是什么:您正在初始化常量“数字”并且从未使用它。所以使用它。

标签: swift calculator cs193p


【解决方案1】:

Swift 被设计为非常安全的语言。除其他外,编译器会检查您声明的变量/常量是否被使用 - 正如 Aurast 在 cmets 中所述,只需使用它。

如果你徘徊,用“_”代替是什么意思,检查这个问题:Swift 2 unused constant warning

【讨论】:

    【解决方案2】:

    这实际上是一个警告,而不是错误,它抱怨的是你从未使用过@ 987654321@初始化后。这样下去,它就会消失。

    消除警告的最简单方法:

    斯威夫特1.2: println("current digit is: \(digit)")

    斯威夫特2: print("current digit is: \(digit)")

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2017-02-20
      • 2018-12-18
      • 2019-07-09
      相关资源
      最近更新 更多