ViewController does not have a member named tipCalc - ios

//
// ViewController.swift
// TipCalculator
//
// Created by Mathias Bakken on 6/28/15.
// Copyright (c) 2015 Mathias Bakken. All rights reserved.
//
I have 6-8 instances where I am receiving the error for not having the member tipCalc. I attempted to make an empty variable, but that only made things worse.
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.
}
}
I am getting a bunch of the same error messages saying "ViewController does not have a member named tipCalc"
I am also receiving the error "Use of unresolved identifier TipCalculatorModel"
How do I fix this issue? Thank you!

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
}
}
I solved the issue by just pasting in the other module. Not a pretty fix. But it works. Thank you.

Related

iOS label not start from the top

My controller allows me to show a text from a song inside a label but I don't know why the gravity of the text is focused at the end. When I want to see the text I always see the song like it's already scrolled. I want to start the song from the top and then scroll it or zoom it.
Every time I want to see a song that is longer than the page I see like it's already scrolled, but if I want to see a song that is shorter than the page it works correctly. How can I fix it??
Here is my code:
import UIKit
class DettaglioCanti: UIViewController {
var dettaglioCanzone: VociMontagna? {
didSet {
configureView()
}
}
var valoriPassati: VociMontagna?
#IBOutlet weak var tv_titolo: UILabel!
#IBOutlet weak var tv_artista: UILabel!
#IBOutlet weak var tv_testoCanzone: UITextView!
#IBAction func btt_note(_ sender: Any) {
startPopUp()
}
#IBOutlet weak var btt_note_2: UIButton!
var pinchGesture = UIPinchGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
configureView()
pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchText(sender:)))
tv_testoCanzone.addGestureRecognizer(pinchGesture)
let range = NSMakeRange(tv_testoCanzone.text.count - 1, 0)
tv_testoCanzone.scrollRangeToVisible(range)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#objc func pinchText(sender: UIPinchGestureRecognizer) {
var pointSize = tv_testoCanzone.font?.pointSize
pointSize = ((sender.velocity > 0) ? 1 : -1) * 1 + pointSize!;
//Definisco i limiti dello zoom per il testo
if (pointSize! < 13) {pointSize = 13}
if (pointSize! > 42) {pointSize = 42}
tv_testoCanzone.font = UIFont( name: "arial", size: (pointSize)!)
}
func configureView() {
if let dettaglioCanzone = dettaglioCanzone {
if let tv_titolo = tv_titolo, let tv_testoCanzone = tv_testoCanzone, let tv_artista = tv_artista {
tv_titolo.text = dettaglioCanzone.titolo
tv_artista.text = dettaglioCanzone.artista
tv_testoCanzone.text = loadFile(file: dettaglioCanzone.nomeTesto)
if (dettaglioCanzone.nomeNota == "0") {btt_note_2.isHidden = true}
}
}
}
func loadFile(file name:String) -> String {
if let path = Bundle.main.path(forResource: name, ofType: "txt") {
if let contents = try? String(contentsOfFile: path) {
return contents
} else {
print("Error! - This file doesn't contain any text.")
}
} else {
print("Error! - This file doesn't exist.")
}
return ""
}
}

Fatal error when unwrapping a segued string (swift3)

This is my code I don't know what I did wrong. All of the problems are in function steve. LebelText is a timer timer label that is segued from another view controller. So i want to take lebetText convert it to a int to subtract 1 from it then reconvert it back to a string to display the number.
This is view Controller a. The texted being segued is lebelText.
import UIKit
class testViewController: UIViewController {
#IBOutlet var lazel: UILabel!
#IBOutlet var plax: UIButton!
#IBOutlet var stopx: UIButton!
var timer = Timer()
var counter = 0.0
var isRunning = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let DestViewController : restultViewController = segue.destination as! restultViewController
DestViewController.LebelText = lazel.text!
}
#IBAction func play(_ sender: Any) {
if !isRunning{
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(testViewController.update), userInfo: nil, repeats: true)
}
plax.isEnabled = false
stopx.isEnabled = true
}
#IBAction func stop(_ sender: Any) {
plax.isEnabled = true
stopx.isEnabled = false
timer.invalidate()
isRunning = false
}
func update(){
counter += 0.1
lazel.text = String(format: "1%f", counter)
lazel.text = "\(counter)"
}}
this is view controller b. The goal is to go to take lebelText convert it to a int to subtracted 1 from it. Then convert it back to a string so it can be displayed.
import UIKit
class restultViewController: UIViewController {
#IBOutlet var dxe: UILabel!
var LebelText = String()
let myInt = Int()
override func viewDidLoad() {
super.viewDidLoad()
steve()
}
func steve(){
var eq = LebelText
var intValue = Int(eq)
let vx = intValue! - 1
let ramit = String(vx)
dxe.text = ramit
}
ok so to get rid of the optional and to be sure it all works you should do like
if let intValue = Int(eq) {
vx = intValue - 1
dxe.text = String(vx)
} else {
//do some stuff if you cannot convert eq to Int
}
but I would recommend you to start with some easier tasks, it looks like you did not completely learn basics.
import UIKit
class restultViewController: UIViewController {
#IBOutlet var someLabel: UILabel!
public var myText: String?
override func viewDidLoad() {
super.viewDidLoad()
self.parseData()
}
private func parseData(){
guard let unwrapedText = self.myText else {
//you didn't pass string
return
}
if let myInt = Int(unwrapedText) {
myInt = myInt - 1
self.someLabel.text = String(myInt)
} else {
//you string is not convertable to int
}
}
}
You Can just do this.
import UIKit
class restultViewController: UIViewController {
#IBOutlet var dxe: UILabel!
var LebelText = String()
let myInt = Int()
override func viewDidLoad() {
super.viewDidLoad()
steve()
}
func steve(){
var eq = Int(LebelText.text)
eq = eq - 1
dxe.text = String(eq)
}

OBD2Kit Swift example can't typecast, keep getting nil

Since there was no example code for using OBD2Kit and Swift I forked it into https://github.com/YannisDC/OBD2Kit and used it as a pod.
I translated some OBJ-C example code but can't seem to downcast the FLWiFiScanTool into the ELM327 type. Why do I keep getting nil?
import UIKit
import OBD2Kit
class ViewController: UIViewController, FLScanToolDelegate {
#IBOutlet weak var hostIpAddress: UITextField!
var scanTool: ELM327!
#IBOutlet weak var statusLabel: UILabel!
#IBOutlet weak var scanToolLabel: UILabel!
#IBOutlet weak var rpmLabel: UILabel!
#IBOutlet weak var speedLabel: UILabel!
#IBOutlet weak var tempLabel: UILabel!
var scanning = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.hostIpAddress.text = "192.168.0.10"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func scanButton(sender: UIButton) {
if !scanning {
startScan()
} else {
stopScan()
}
}
func startScan() {
if let scanTool = ELM327(host: self.hostIpAddress.text!, andPort: 35000) {
self.statusLabel.text = "Initializing..."
scanTool.useLocation = true
scanTool.delegate = self
scanTool.startScanWithSensors({() -> [AnyObject] in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.statusLabel.text = "Scanning..."
self.scanToolLabel.text = scanTool.scanToolName
})
// let sensors: [AnyObject] = [ OBD2Sensor.SensorEngineRPM as! AnyObject,
// OBD2Sensor.SensorVehicleSpeed as! AnyObject,
// OBD2Sensor.SensorOxygenSensorsPresent as! AnyObject ]
let sensors: [AnyObject] = [ 0x0C,
0x0D,
0x13 ]
return sensors
})
self.scanTool = scanTool
print("So far succesfull")
scanning = !scanning
} else {
self.statusLabel.text = "Not working"
}
}
func stopScan() {
statusLabel.text = "Stopped"
let scanTool: ELM327 = self.scanTool
scanTool.cancelScan()
scanTool.sensorScanTargets = nil
scanTool.delegate = nil
scanning = !scanning
}
// MARK: - FLScanToolDelegate
func scanTool(scanTool: FLScanTool, sensor: FLECUSensor) {
var sensorLabel: UILabel? = nil
switch sensor.pid {
case OBD2Sensor.SensorEngineRPM:
sensorLabel = self.rpmLabel
case OBD2Sensor.SensorVehicleSpeed:
sensorLabel = self.speedLabel
default:
sensorLabel = self.tempLabel
}
self.showSensorValue(sensor, onLabel: sensorLabel!)
}
func showSensorValue(sensor: FLECUSensor, onLabel label: UILabel) {
let sensorValue: String = "\(sensor.valueStringForMeasurement1(false)) \(sensor.imperialUnitString)"
dispatch_async(dispatch_get_main_queue(), {() -> Void in
label.text = sensorValue
})
}
}
Edit 1:
I can already scan for the tool now since ELM327 is a FLWiFiScanTool and not the other way around. I can get the toolname so it's connecting but can't seem to get the sensors output.
I managed to figure it out. The expected sensors array should be one of NSNumbers so I casted the sensors UInt's to NSNumbers.
And I also missed the didUpdateSensor part in the scanTool function.
Make sure to use the metric system, this is somehow way more reliable in my case. (My car is using the metric system as well, maybe that's why.)
import UIKit
import OBD2Kit
class ViewController: UIViewController, FLScanToolDelegate {
var scanTool: ELM327!
#IBOutlet weak var statusLabel: UILabel!
#IBOutlet weak var scanToolLabel: UILabel!
#IBOutlet weak var rpmLabel: UILabel!
#IBOutlet weak var speedLabel: UILabel!
#IBOutlet weak var tempLabel: UILabel!
var scanning = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func scanButton(sender: UIButton) {
if !scanning {
startScan()
} else {
stopScan()
}
}
func startScan() {
if let scanTool = ELM327(host: "192.168.0.10", andPort: 35000) {
self.statusLabel.text = "Initializing..."
scanTool.useLocation = true
scanTool.delegate = self
scanTool.startScanWithSensors({() -> [AnyObject] in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.statusLabel.text = "Scanning..."
self.scanToolLabel.text = scanTool.scanToolName
})
let sensors: [AnyObject] = [ OBD2Sensor.SensorEngineRPM as NSNumber, OBD2Sensor.SensorVehicleSpeed as NSNumber, OBD2Sensor.SensorOxygenSensorsPresent as NSNumber ]
return sensors
})
self.scanTool = scanTool
print("So far succesfull")
scanning = !scanning
} else {
self.statusLabel.text = "Not working"
}
}
func stopScan() {
statusLabel.text = "Stopped"
let scanTool: ELM327 = self.scanTool
scanTool.cancelScan()
scanTool.sensorScanTargets = nil
scanTool.delegate = nil
scanning = !scanning
}
// MARK: - FLScanToolDelegate
func scanTool(scanTool: FLScanTool!, didUpdateSensor sensor: FLECUSensor!) {
var sensorLabel: UILabel? = nil
switch sensor.pid {
case OBD2Sensor.SensorEngineRPM:
sensorLabel = self.rpmLabel
case OBD2Sensor.SensorVehicleSpeed:
sensorLabel = self.speedLabel
default:
sensorLabel = self.tempLabel
}
self.showSensorValue(sensor, onLabel: sensorLabel!)
}
func showSensorValue(sensor: FLECUSensor, onLabel label: UILabel) {
let sensorValue: String = "\(sensor.valueStringForMeasurement1(true)) \(sensor.metricUnitString)"
dispatch_async(dispatch_get_main_queue(), {() -> Void in
label.text = sensorValue
})
}
}

Swift bad instruction error shopping list

I have a problem, Xcode gives me this error "EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode==0*0)" while I'm trying to make my buttons delete indexes in my array, "shoppingList".
Please help me and tell me what i did wrong so i can improve later on.
//
// ViewController.swift
// ShoppingList
//
// Created by Petr Chrastek on 29/03/16.
// Copyright © 2016 ACS. All rights reserved.
//
class ViewController: UIViewController {
#IBOutlet weak var labelText: UILabel!
#IBOutlet weak var label0: UILabel!
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
var shoppingList = ["eggs", "milk", "cake", "sugar"]
#IBAction func remove0(sender: UIButton) {
shoppingList.removeAtIndex(0)
}
#IBAction func remove1(sender: UIButton) {
shoppingList.removeAtIndex(1)
}
#IBAction func remove2(sender: UIButton) {
shoppingList.removeAtIndex(2)
}
#IBAction func remove3(sender: UIButton) {
shoppingList.removeAtIndex(3)
}
override func viewDidLoad() {
super.viewDidLoad()
let str: String? = shoppingList[0]
let str1: String? = shoppingList[1]
let str2: String? = shoppingList[2]
let str3: String? = shoppingList[3]
let count = shoppingList.count
labelText.text? = "you are missing \(count) items"
if str != nil {
label0.text? = "\(str)"
} else {
label0.text? = "empty"
}
if str1 != nil {
label1.text? = "\(str1)"
} else {
label1.text? = "empty"
}
if str2 != nil {
label2.text? = "\(str2)"
} else {
label2.text? = "empty"
}
if str3 != nil {
label3.text? = "\(str3)"
} else {
label3.text? = "empty"
}
// 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.
}
}
After you delete the first item from your list, shoppingList will only have 3 items in it, so accessing shoppingList[3] will crash (remember with 3 items only 0..<2 are valid.
The easiest way to resolve the problem is to use the following pattern so you're checking the count to make sure indices are valid before using them.
if shoppingList.count > 0 {
label0.text = shoppingList[0]
} else {
label0.text = "empty"
}
if shoppingList.count > 1 {
label1.text = shopingList[1]
} else {
label1.text = "empty"
}
I've made some additional changes as well, such as not pointless using string interpolation to turn a String into the same String, since [String][n] will always return a String (never a String?) there's no need to deal with the Optionals
You'll have similar problems (in fact, probably what you're running into now) when you try to:
shoppingList.removeAtIndex(3)
the second time, since 3 is no longer a valid index, instead, use:
if shoppingList.count > 3 {
shoppingList.removeAtIndex(3)
}

Fatal Error creating Array

This is the code I have. Basically when the user first chooses the quality of the dinning whether it is fine (20%), casual (15%), or a bar (11%). From there the user will then be asked on a 1-5 scale about the food quality, service, etc. Depending what number they choose, for example if the fine dinning was a 5/5 then it will ad 4% to the already 20 percent (each number will have a selected value to add to the tip). Can anyone figure out what's going on? Or if there is a better way about doing it.
import UIKit
#IBDesignable
class ViewController: UIViewController {
#IBOutlet weak var billTotal: UITextField!
var diningValue: Double = 0.0
#IBAction func toggleFineDining(sender: UIButton) {
sender.highlighted = sender.selected
if sender.selected == true{
diningValue = 0.20
}
}
#IBAction func toggleCasualDining(sender: UIButton) {
sender.highlighted = sender.selected
if sender.selected == true{
diningValue = 0.15
}
}
#IBAction func toggleBartender(sender: UIButton) {
sender.highlighted = sender.selected
if sender.selected == true{
diningValue = 0.11
}
}
#IBOutlet weak var service: UITextField!
#IBOutlet weak var friendliness: UITextField!
#IBOutlet weak var foodQuality: UITextField!
#IBOutlet weak var atmosphere: UITextField!
#IBOutlet weak var overall: UITextField!
#IBOutlet weak var recommend: UISwitch!
#IBOutlet weak var total: UILabel!
func totalBill(inputArray: [Double], value: Double, recommend: Bool)->Double{
var array: [Double] = []
let total = inputArray[0]
for var i = 1; i < inputArray.count; ++i {
array.append(inputArray[i] * 0.01)
}
let sum = array.reduce(0, combine: +)
var totalTip = Double()
if recommend == true{
totalTip = sum + value
} else {
if sum < 0.15 {
totalTip = 0.15
} else {
totalTip = sum + value
}
}
let tipValue = totalTip * total
return total + tipValue
}
#IBAction func calcTotal(sender: AnyObject) {
var valueList = [String(service.text), String(friendliness.text), String(foodQuality.text), String(atmosphere.text), String(overall.text)]
let list = doubleArray(valueList)
print(list)
let initialTotal = Double(billTotal.text!)
let total_bill = totalBill(list, value: diningValue, recommend: recommend.selected)
print("Total: ", total_bill)
print(String(format: "%.3f", Double(total_bill)))
total.text = "$" + String(format: "%.2f", Double(total_bill))
//diningValue = diningValue
}
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.
}
}
func doubleArray(list: [String])->[Double]{
var doubledList: [Double] = []
for element in 0...list.count{
let doubledElement = Double(list[element])
doubledList.append(doubledElement!)
}
return doubledList
}
Your issue is in the last function:
func doubleArray(list: [String])->[Double]{
var doubledList: [Double] = []
for element in 0...list.count{
let doubledElement = Double(list[element])
doubledList.append(doubledElement!)
}
return doubledList
}
Your loop is looping from 0 to list.count and it crashes at list[element] when element is list.count (Out of Index)
You need to change from 0...list.count to 0..<list.count:
func doubleArray(list: [String])->[Double]{
var doubledList: [Double] = []
for element in 0..<list.count{
let doubledElement = Double(list[element])
doubledList.append(doubledElement!)
}
return doubledList
}
I would recommend you to do it in swifty way:
func doubleArray(list: [String])->[Double]{
var doubledList: [Double] = []
for element in list {
doubledList.append(Double(element) ?? 0.0)
}
return doubledList
}
A shorter version using map function:
func doubleArray(list: [String])->[Double]{
return list.map( { Double($0) ?? 0.0 })
}

Resources