can't convert string into int to display on label

import UIKit
class cuenta: UIViewController {

    @IBOutlet weak var displaytotal: UILabel!
    @IBOutlet weak var display: UILabel!
    var cubitoCHocolate: String!
    var cubitoCoco: String!
    var cubitoCacahuate: String!
    var cubitoFresa: String!
    var cubitoNaranja: String!
    var cubitoMango: String!
    var cuentaDeCubitos = String /*Expected member name or constructor call after type name*/


    override func viewDidLoad() {
        super.viewDidLoad()
  
        cuentaDeCubitos = cubitoCHocolate + cubitoCoco + cubitoCacahuate + cubitoFresa + cubitoNaranja + cubitoMango /*Expected member name or constructor call after type name*/

        display.text = "Tu pedido:" + "\n" + "Chocolate: "+cubitoCHocolate+"\n"+"Coco: "+cubitoCoco+"\n"+"Cacahuate: "+cubitoCacahuate+"\n"+"Fresa: "+cubitoFresa+"\n"+"Naranja: "+cubitoNaranja+"\n"+"Mango: "+cubitoMango
       if Int(cuentaDeCubitos) >= 1 { /*Cannot invoke initializer for type 'Int' with an argument list of type '(String.Type)'*/
            displaytotal.text = Int(cuentaDeCubitos) * 6 /*Cannot invoke initializer for type 'Int' with an argument list of type '(String.Type)'*/

        }
       else {
        displaytotal.text = "0"
        }
    }

can't convert string into integer to show in my label call 'displaytotal' i try many diferrents ways to fix it by readding the forum but i simple can't plz help

When does the crash occurs ? When you click on a continuar button ?


At which line exactly does it crash ?


The crash mentions a venta object. Where is it defined ?


Note:

there was a typo on line

cuentita.cubitoCoco = Int(coco.tex ?? "0") ?? 0


Should read

cuentita.cubitoCoco = Int(coco.text ?? "0") ?? 0

yeah my mistake, there was an error from the button it wasn't liked to the next viewcontroller, fixed



//youtu.be/5KWhsGP8CYA



i upload the test from the app,

1 - first view controller called "Home" right on the middle top


you select the location where you are located

( i don't have a variable to save the location yet)


2 - second view controller called "productos"(items class)

the user must select the products listed, and hit 'continuar button'


Chocolate 6$ (stepper) 0. // products are defined and saved

@IBAction funcchocolatecounter(_ sender: UIStepper) {
        chocolate.text = String(sender.value) //defined by sender.value and saved in chocolate.text
    }

Coco 6$ (stepper) 0

Cacahuate 6$ (stepper) 0

Fresa 6$ (stepper) 0

Naranja 6$ (stepper) 0

Mango 6$ (stepper) 0


value saved and prepare for segue sent to "cuenta" class

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard let cuentita = segue.destination as? cuenta  else { return  }
        cuentita.cubitoCHocolate = Int(chocolate.text ?? "0") ?? 0
        cuentita.cubitoCoco = Int(coco.text ?? "0") ?? 0
        cuentita.cubitoCacahuate = Int(cacahuate.text ?? "0") ?? 0
        cuentita.cubitoFresa = Int(fresa.text ?? "0") ?? 0
        cuentita.cubitoNaranja = Int(naranja.text ?? "0") ?? 0
        cuentita.cubitoMango = Int(mango.text ?? "0") ?? 0
    }



3 - third view controller called "cuenta"(cuenta class)


down below where


"Tu pedido:

Chocolate: 0

Coco: 0

Cacahuate: 0

Fresa: 0

Naranja: 0

Mango: 0


int variables defined

var cubitoCHocolate: Int = 0
    var cubitoCoco: Int = 0
    var cubitoCacahuate: Int = 0
    var cubitoFresa: Int = 0
    var cubitoNaranja: Int = 0
    var cubitoMango: Int = 0
    var cuentaDeCubitos: Int = 0




func for display the sender.value (stepper from 'items' class)


override func viewDidLoad() {
        super.viewDidLoad()
        confirmarCompra.isEnabled = false
        cuentaDeCubitos = cubitoCHocolate + cubitoCoco + cubitoCacahuate + cubitoFresa + cubitoNaranja + cubitoMango // all the products are added and saved in cuentaDeCubitos
       
        display.text = "Tu pedido:" + "\n" + "Chocolate: \(cubitoCHocolate)"+"\n"+"Coco: \(cubitoCoco)"+"\n"+"Cacahuate: \(cubitoCacahuate)"+"\n"+"Fresa: \(cubitoFresa)"+"\n"+"Naranja: \(cubitoNaranja)"+"\n"+"Mango: \(cubitoMango)"
// it suppose to show the sender.value(stepper from items class)
       
        if cuentaDeCubitos >= 1 {
            displaytotal.text = "Tu total a pagar es: \(cuentaDeCubitos * 6)” //every variable must be multiplied by 6 * cuentaDeCubitos (6 is the cost of the product)
        }
        else {
            displaytotal.text = "" //if the user doesn’t select any product doesn’t show anything
        }
    }





it supposed to work like that because int variables can be multiplied by int variables.

but they don't, it doesn't show any errors

looks like the the app doesn't pass data from the "items" to "cuenta" viewcontroller.

Accepted Answer

In the following:


Chocolate 6$ (stepper) 0. // products are defined and saved

@IBAction func chocolatecounter(_ sender: UIStepper) {
        chocolate.text = String(sender.value) //defined by sender.value and saved in chocolate.text
    }

Coco 6$ (stepper) 0

Cacahuate 6$ (stepper) 0

Fresa 6$ (stepper) 0

Naranja 6$ (stepper) 0

Mango 6$ (stepper) 0


Note: it is func chocolatecounter not funcchocolatecounter

sender.value is Double, hence you get 0

So you must first convert text to Double, then to Int


You should write:


        cuentita.cubitoCHocolate = Int( Double(chocolate.text ?? "0") ?? 0)


or change func chocolatecounter like this

@IBAction func chocolatecounter(_ sender: UIStepper) {
        chocolate.text = String(Int(sender.value))      //Now chocolate.text is just 3 and not 3.0
    }
// and of course keepcuentita.cubitoCHocolate like this in that case
cuentita.cubitoCHocolate = Int(chocolate.text ?? "0") ?? 0



Note: you show Chocolate 6$

Is 6$ what is inside chocolate.text. It should not, otherwise the text -> Int conversion must be modified.

damm man you're a god, is now working but



youtu.be/atnvb6I8DBc



cuentita.cubitoCHocolate = Int(chocolate.text ?? "0") ?? 0

is the only displaying variable



it might be because of


display.text = "Tu pedido:" + "\n" + "Chocolate: \(cubitoCHocolate)"+"\n"+"Coco: \(cubitoCoco)"+"\n"+"Cacahuate: \(cubitoCacahuate)"+"\n"+"Fresa: \(cubitoFresa)"+"\n"+"Naranja: \(cubitoNaranja)"+"\n"+"Mango: \(cubitoMango)"


i think the app concatenate all the variables and added only the last one

override func viewDidLoad() {
        super.viewDidLoad()
        confirmarCompra.isEnabled = false
        cuentaDeCubitos = cubitoCHocolate + cubitoCoco + cubitoCacahuate + cubitoFresa + cubitoNaranja + cubitoMango
       
       
       display.text = "Tu pedido:" + "\n" + "Chocolate: \(cubitoCHocolate)"+"\n"+"Coco: \(cubitoCoco)"+"\n"+"Cacahuate: \(cubitoCacahuate)"+"\n"+"Fresa: \(cubitoFresa)"+"\n"+"Naranja: \(cubitoNaranja)"+"\n"+"Mango: \(cubitoMango)"
       
        if cuentaDeCubitos >= 1 {
            totalapagar.text = "Tu total a pagar es: \(cuentaDeCubitos * 6)$"
        }
        else {
            totalapagar.text = ""
        }
    }

You mean that you only get :

Tu pedido:

Chocolate: 5


and not the others ?


Did you EITHER modifiy all the other IBAction in the same way ?

   @IBAction func cococounter(_ sender: UIStepper) { 
        coco.text = String(Int(sender.value))
    } 
    @IBAction func cacahuatecounter(_ sender: UIStepper) { 
        cacahuate.text = String(Int(sender.value)) 
    } 
    @IBAction func fresacounter(_ sender: UIStepper) { 
        fresa.text = String(Int(sender.value))
    } 
    @IBAction func naranjacounter(_ sender: UIStepper) { 
        naranja.text = String(Int(sender.value)) 
    } 
    @IBAction func mangocounter(_ sender: UIStepper) { 
        mango.text = String(Int(sender.value))
    }


OR CHANGE all the other like:

       cuentita.cubitoCoco = Int( Double(coco.text ?? "0") ?? 0)
       cuentita.cubitoCacahuate = Int( Double(cacahuate.text ?? "0") ?? 0)
// etc…



If you did this, maybe your label is too small and there are a lot of \n

Just for the test, delete them and tell what you get :


      display.text = "Tu pedido:"  + "Chocolate: \(cubitoCHocolate)"+"Coco: \(cubitoCoco)"+"Cacahuate: \(cubitoCacahuate)"+"esa: \(cubitoFresa)"+"Naranja: \(cubitoNaranja)"+"Mango: \(cubitoMango)"

If that is effectively the problem, go to interface builder and change the number of lines for the label (attributes inspector)


Hope it will work this time. If so, don't forget to mark correct answer to close this long thread.


Oh, a last indiscrete question. You speak spanish. Are you in Europe or south America, or US ?

thanks for walk me through, this app got me headache

the problem was i didn't update


all the other products

cuentita.cubitoCHocolate = Int(chocolate.text ?? "0") ?? 0



every one was named as "cubitoCHocolate" and not like supposed to be named


and yeah, i speak spanish i'm from México south america

can't convert string into int to display on label
 
 
Q