1

I have a button that adds 1 to an a attempt score label and then a button that adds a 1 or 0 to the correct score label depending which button is pressed. This data is then passed to various viewControllers. On the last viewController i want to convert the string from the label into a int and then do a simply calculation in order to get a percentage. The code i have tried is below. The percentage is not being displayed so I do not think i am converting it correctly?

On previous VC the aScore and cScore are set as variable that = 0. For example..

var aScore = 0
var cScore = 0

I am passing the data between VC and then on the final viewController the following code exits.

import UIKit

class gViewController: UIViewController {


@IBOutlet weak var correctScore: UILabel!
@IBOutlet weak var attemptScore: UILabel!
@IBOutlet weak var percentage: UILabel!

var aScore: Int!
var cScore: Int!
var percentage1: Int!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    correctScore.text = NSString(format: "%i", cScore) as String
    attemptScore.text = NSString(format: "%i", aScore) as String

    let c:Int? = Int(correctScore.text!)
    let a:Int? = Int(attemptScore.text!)

    percentage1 = c!/a!

    percentage.text = NSString(format: "%i", percentage1) as String
}

suggested code

  • can you do a `print` of `a` and `c` value before the `percentage1`? then `print` the `percentage1` value and `percentage` value? you need to make sure your variable has value first. – f_qi Mar 28 '17 at 18:06
  • why do you first convert `Int` to `NSString` and then `NSString` to `Int`? Why do not you just `cScore/aScore` – JuicyFruit Mar 28 '17 at 18:14
  • on the viewControllers before the final one i have set cScore and aScore to 0 using the code: Var aScore = 0. I have edited the post above. Hence why i tried to convert them to int – Keiron Morris Mar 28 '17 at 19:24

1 Answers1

0

Try this

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.correctScore.text = String(format: "%.1f", cScore)
    self.attemptScore.text = String(format: "%.1f", aScore)

    var percentage1 = Float(cScore)/Float(aScore)

    self.percentage.text = String(format: "%.1f", percentage1)

}

Lepidopteron
  • 5,468
  • 5
  • 37
  • 49
f_qi
  • 499
  • 4
  • 20
  • on the previous viewControllers the following code exists: var aScore = 0 var cScore = 0 . therefore when i get to do the percentage the code you have given does not work. I have edited the original post with a picture of what happens. – Keiron Morris Mar 28 '17 at 19:18
  • You do have a variable aScrote and cScore of type int. for division, make sure they are interpreted as float or double for dividing them. – Lepidopteron Mar 28 '17 at 19:41
  • @KeironMorris What do you mean by previous viewController? Are you passing the data from one VC to another VC? – f_qi Mar 28 '17 at 19:50
  • yes i am passing data from one VC to another VC using a Segue.. @f_qi – Keiron Morris Mar 29 '17 at 07:03
  • @KeironMorris When you pass data, make sure the data did pass to your destination VC. – f_qi Mar 29 '17 at 13:29
  • @f_qi i am using the following code to pass the data.. https://stackoverflow.com/questions/42612328/saving-data-from-a-label-on-multiple-view-controllers – Keiron Morris Apr 03 '17 at 12:03