//
// ViewController.swift
// TipCalculator
//
// Created by Mathias Bakken on 6/28/15.
// Copyright (c) 2015 Mathias Bakken. All rights reserved.
//
我有6-8个实例,我收到错误,因为没有成员tipCalc。我试图制作一个空变量,但这只会让事情变得更糟。
import UIKit
class ViewController: UIViewController {
@IBOutlet var totalTextField : UITextField!
@IBOutlet var servQualitySlider : UISlider!
@IBOutlet var foodQualitySlider : UISlider!
@IBOutlet var servQualityLabel : UILabel!
@IBOutlet var foodQualityLabel : UILabel!
@IBOutlet var resultsTextView : UITextView!
@IBAction func calculateTapped(sender : AnyObject){
tipCalc.total = Double((totalTextField.text as NSString).doubleValue)
let possibleTips = tipCalc.returnPossibleTips()
var results = ""
for (tipPct, tipValue) in possibleTips{
results += "\(tipPct)%: \(tipValue)\n"
}
resultsTextView.text = results
}
@IBAction func servQualityChanged(sender : AnyObject){
tipCalc.servQuality = Double(servQualitySlider.value)/100.0
refreshUI()
}
@IBAction func foodQualityChanged(sender : AnyObject){
tipCalc.foodQuality = Double(foodQualitySlider.value)/100.0
refreshUI()
}
@IBAction func viewTapped(sender : AnyObject){
totalTextField.resignFirstResponder()
}
let tipCalc = TipCalculatorModel(total: 33.25, foodQuality: 0.06, servQuality: 0.06)
func refreshUI(){
totalTextField.text = String(format: "%0.2f", tipCalc.total)
foodQualitySlider.value = Float(tipCalc.foodQuality) * 100.0
servQualitySlider.value = Float(tipCalc.servQuality) * 100.0
foodQualityLabel.text = "Food Quality 1-10 (\(Int(foodQualitySlider.value))%)"
servQualityLabel.text = "Service Quality 1-10 (\(Int(servQualitySlider.value))%)"
resultsTextView.text = ""
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
refreshUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
} 我收到一堆相同的错误消息说" ViewController没有名为tipCalc&#34的成员;
我也收到错误"使用未解析的标识符TipCalculatorModel"
如何解决此问题?谢谢!
答案 0 :(得分:0)
import Foundation
class TipCalculatorModel{
var total: Double
var foodQuality: Double
var servQuality: Double
var subtotal: Double{
get{
return total / (foodQuality + servQuality + 1)
}
}
init(total: Double, foodQuality: Double, servQuality: Double){
self.total = total
self.foodQuality = foodQuality
self.servQuality = servQuality
}
func calcTipWithTipPct(foodQuality: Double, servQuality: Double)->Double{
return subtotal * (foodQuality + servQuality)
}
func returnPossibleTips()->[Int:Double]{
let possibleTipsInferred = [0.15, 0.18, 0.20]
let possibleTipsExplicit:[Double] = [0.15, 0.18, 0.20]
var retval = [Int:Double]()
for possibleTip in possibleTipsInferred{
let intPct = Int(possibleTip*100)
retval[intPct] = calcTipWithTipPct(foodQuality, servQuality: servQuality)
}
return retval
}
}
我通过粘贴其他模块解决了这个问题。不是很好的修复。但它的确有效。谢谢。