使用Swift时出错-实例成员不能在'ViewController'类型上使用

用户名

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

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

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

这是我的完整代码。

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) {


    }
}
阿森

每次您调用该代码时,都会返回一个新值。我期望它是您想要的

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

代替

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

实例成员不能在'ViewController'类型上使用

错误:实例成员不能在类型'ViewController'上使用

实例成员不能在类型上使用

Swift实例成员不能在类型上使用

Swift:实例成员不能在类型上使用

实例成员“图层”不能在类型“ CustomPrettyView”上使用

如何修复“实例成员'坐标'不能在类型'MKMapPoint'上使用”的问题,在Swift 5.0中

Swift 3:实例成员不能在“ View Controller”类型上使用

Swift 2.0地图“实例成员不能在类型上使用”

这里出了什么问题:实例成员不能在类型上使用

编码SpriteKit游戏,我收到“不能在类型'CGRect上使用实例成员'minX'”

实例成员...不能用于类型 ViewController

在自定义类中初始化日期会导致错误:“实例成员不能在类型上使用”

无法将变量传递到一个特定的类中,实例成员不能在类型错误时使用

不能在属性初始值设定项中使用实例成员,将变量设置为惰性不起作用

使用Storyboard时实例化ViewController

“ViewController”类型的值没有成员“ref”;您指的是 'self' 吗?使用 firebase 快速出错

推送UIViewController时无法在类型上使用实例成员

Swift:不能在属性初始值设定项中使用实例成员“trialGame”;属性初始值设定项在 'self' 可用之前运行

重用的GMSMapView可以在首次使用时使用,但不能在任何其他ViewController上使用

swift:不能在不可变值上使用可变成员:下标是仅获取的

为什么不能在成员函数中使用这种模式?

不能在属性初始化程序中使用实例成员“ country”;属性初始化程序在“自我”可用之前运行

不能在属性初始值设定项中使用实例成员“月”;属性初始值设定项在 'self' 可用之前运行

Swift 错误“静态成员不能用于类型的实例”

Swift - 静态成员动画不能用于 uiview 类型的实例

Swift 3:实例成员'CognitoPoolID'不能用于类型'Constants'

在Swift中对我自己的ViewController进行单元测试时,使用未声明的类型'ViewController'吗?

Swift 4上带有嵌套类的“实例成员不能用于类型”错误