ios 使用 Swift 时出错 - 实例成员不能用于类型“ViewController”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33055147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:57:39  来源:igfitidea点击:

Error using Swift - Instance member cannot be used on type 'ViewController'

iosswift

提问by Scriptable

I've been trying to make an app in Xcode 7 using Swift and I'm trying to convert numbers inside a textfield to an integer using this code, let a = Int(percentLabel.text!)

我一直在尝试使用 Swift 在 Xcode 7 中制作一个应用程序,我正在尝试使用此代码将文本字段中的数字转换为整数, let a = Int(percentLabel.text!)

But everytime I try this I get an error that says Instance Member 'percentLabel' cannot be used on type 'ViewController'

但是每次我尝试这个时,我都会收到一个错误消息 Instance Member 'percentLabel' cannot be used on type 'ViewController'

I'm not sure why I'm getting this error, maybe it's a bug which I've already found in Xcode 7.

我不确定为什么我会收到这个错误,也许这是我在 Xcode 7 中发现的一个错误。

Here is my full code.

这是我的完整代码。

import UIKit

class ViewController: UIViewController {
    @IBOutlet var tipSlider: UISlider!
    @IBOutlet var splitSlider: UISlider!
    @IBOutlet var billAmount: UITextField!
    @IBOutlet var totalLabel: UILabel!
    @IBOutlet var tipTotal: UILabel!
    @IBOutlet var totalPerPerson: UILabel!
    @IBOutlet var percentLabel: UILabel!
    @IBOutlet var splitLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    let a = Int(percentLabel.text!)

    @IBAction func tipChanged(sender: AnyObject) {
        let currentValue = Int(tipSlider.value)
        percentLabel.text = "\(currentValue)"
    }

    @IBAction func splitChanged(sender: AnyObject) {
        let currentValue = Int(splitSlider.value)
        splitLabel.text = "\(currentValue)"
    }

    @IBAction func amountChanged(sender: AnyObject) {


    }
}

采纳答案by Arsen

This code returns a new value every time you call it. I expect it what you're looking for

每次调用此代码时都会返回一个新值。我期待它是你正在寻找的

var myInt: Int? {
    return Int(percentLabel.text!)
}

instead of

代替

let myString = percentLabel
let myInt: Int? = Int(myString)

回答by Scriptable

You cannot have these two lines outside of a function call in a class

您不能在类中的函数调用之外使用这两行

let myString = percentLabel
let myInt: Int? = Int(myString)

You cannot intialise a property to a value of another property, you cannot set a variable's default value to that of another property. you would usually do this in a function like viewDidLoad or viewWillAppear. Arsen's answer should also work as it will try to get the value lazily, or as you request it.

您不能将一个属性初始化为另一个属性的值,也不能将变量的默认值设置为另一个属性的默认值。您通常会在 viewDidLoad 或 viewWillAppear 之类的函数中执行此操作。Arsen 的答案也应该有效,因为它会尝试懒惰地获取价值,或者按照您的要求获取价值。

let myString = percentLabelwould also not return the text, you'd need to do let myString = percentLabel.textbut you cannot assign this value like a property. you'll need to put his code in the body of a function

let myString = percentLabel也不会返回文本,您需要这样做,let myString = percentLabel.text但不能像属性一样分配此值。你需要把他的代码放在函数体中