简体   繁体   中英

Swift 2 Expected Declaration error & instance member cannot be used on view controller

Here is an screenshot of the code:

图片

The image shows a more clear view, so you better see it

import UIKit

class ViewController: UIViewController {
// Inputs
    @IBOutlet weak var billField: UITextField!
//Outputs
    @IBOutlet weak var tipPercentageLabel: UILabel!
    @IBOutlet weak var personsLabel: UILabel!
    @IBOutlet weak var tipTotalLabel: UILabel!
    @IBOutlet weak var tipPerPerson: UILabel!
    @IBOutlet weak var GrandTotalLabel: UILabel!
    @IBOutlet weak var totalPerPerson: UILabel!
//Slider
    @IBOutlet weak var tipSlider: UISlider!
    @IBOutlet weak var personsStepper: UIStepper!
// Variables\
    var amountDouble = Double(50)
    var sliderValue = 18
    var tipPerc = 0.18
    var tip = Double(10)
    var myTip = Double(10)
    var persons = Int(1)



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        GrandTotalLabel.text = "Grand Total"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func tipSliderChanged(sender: UISlider) {
        print("tip adjusted")

        sliderValue = Int(sender.value)
        tipPerc = Double(sliderValue) * 0.01
        tipPercentageLabel.text = "\(sliderValue)%"

    }
    @IBAction func personsStepper(sender: UIStepper) {
        print("persons adjusted")

        persons = Int(sender.value)
        personsLabel.text = "\(persons)"
    }





    let pctFormatter = NSNumberFormatter()
    pctFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle

THE FIRST ERROR IN THE LINE ABOVE THAT BEGINS WITH "pctFormatter", the error is "EXPECTED DECLARATION"

    amountDouble = (billField.text as NSString).doubleValue

    tip = amountDouble * tipPerc

    tipPercentageLabel.text = "(Int(sliderValue))%"
    tipTotalLabel.text = pctFormatter.stringFromNumber(tip)
    var result = Double(amountDouble)+Double(tip) 

THE SECOND ERROR IS IN THE LINE ABOVE AND THE ERROR SAYS "INSTANCE MEMBER 'amountDouble' CANNOT BE USED ON TYPE 'View Controller'

    GrandTotalLabel.text = pctFormatter.stringFromNumber(tip/personsDouble) 

AND THE LAST ONE IS IN THE LINE ABOVE AND SAYS EXPECTED DECLARATION

    myTip = Double(tip/personsDouble)
    totalPerPerson.text = pctFormatter.stringFromNumber(result / personsDouble)

}

The problem is that you have lines of code that are not within a method. That code is just sitting inside the class definition and the compiler doesn't know what to make of that outside of a func implementation.

By the way, if your intent was to define a property that is initialized with code, you could do something like:

let pctFormatter: NSNumberFormatter = {
    let _formatter = NSNumberFormatter()
    _formatter.numberStyle = .CurrencyStyle
    return _formatter
}()

But the other lines of code below that pctFormatter were clearly intended to be put within a method, so put those with a func implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM