Swift一元运算符错误

时间:2017-02-23 20:14:44

标签: swift

为什么当我有一行代码时呢?

MoneyScreen1.text = "£" + String(moneyConstants.money) + ".00"

然而,在我的项目的另一个文件中,我有:

moneyLabel.text = "£" + String(moneyConstants.money) + ".00"

但我得到两个错误:

Consecutive statements on a line must seperated with a ';'

Unary operator '+' cannot be applied to an operand of type 'String'

但除了正在编辑的标签的不同名称之外,该行几乎完全相同。任何帮助将不胜感激。

编辑:

全功能:

func checkValues() {
    moneyLabel.text = "£" + String(moneyConstants.money) + ".00"
    let savedValues = UserDefaults.standard
    savedValues.setValue(moneyConstants.money, forKey: "money")
    savedValues.setValue(buildingLevels.jitterClickNumber, forKey:"jitterClickBuildingLevel")
    savedValues.setValue(buildingLevels.macrosNumber , forKey:"macrosBuildingLevel")
    savedValues.setValue(buildingLevels.autoClickerNumber , forKey:"autoClickerBuildingLevel")
    savedValues.setValue(buildingLevels.anotherPCNumber, forKey:"anotherPCBuildingLevel")
    savedValues.setValue(buildingLevels.workerNumber , forKey:"workerBuildingLevel")
    savedValues.setValue(buildingLevels.houseNumber , forKey:"houseBuildingLevel")
    savedValues.setValue(buildingLevels.officeNumber , forKey:"officeBuildingLevel")
    savedValues.setValue(buildingLevels.skyriseNumber , forKey:"skyriseBuildingLevel")
    savedValues.setValue(buildingLevels.planetNuber , forKey:"planetBuildingLevel")
    jitterClickCostLabel.text = "£" + String(buildingConstants.jitterClickConstantCost) + ".00"
    macrosCostLabel.text = "£" + String(buildingConstants.macrosConstantCost) + ".00"
    autoClickerCostLabel.text = "£" + String(buildingConstants.autoClickerConstantCost) + ".00"
    anotherPCCostLabel.text = "£" + String(buildingConstants.anotherPCConstantCost) + ".00"
    workerCostLabel.text = "£" + String(buildingConstants.workerConstantCost) + ".00"
    houseCostLabel.text = "£" + String(buildingConstants.houseConstantCost) + ".00"
    officeCostLabel.text = "£" + String(buildingConstants.officeConstantCost) + ".00"
    skyriseCostLabel.text = "£" + String(buildingConstants.skyriseConstantCost) + ".00"
    planetCostLabel.text = "£" + String(buildingConstants.planetConstantCost) + ".00"
    moneyLabel.text = "£" + String(moneyConstants.money) + ".00"
}

标签声明:

@IBOutlet var moneyLabel: UILabel!

1 个答案:

答案 0 :(得分:1)

我怀疑这个问题是由加号后缺少空格引起的,在某种程度上你没有向我们展示。我可以使用以下代码召唤完全相同的错误消息:

    func checkValues() {
        let s = "2" +String(100)
    }

以下是显示错误的屏幕截图:

enter image description here

所以我的猜测是你正在做那样的事情。