0

I have an assignment to make a calculator to calculate the average number of given inputs. I will need to accept and display number entered in the range between 0 and 100 (‘ADD’ button). And calculate and display the average.

There is a UITextField where i enter the numbers , then press the ADD button to display it into a label/textview (im not sure which to use).

The numbers should be appended into an array so as to use the averageOf() function.

I've tried displaying the numbers but the label replace the new number instead of adding it. Im very new to swift and not sure how to code to accept only a certain range of number.

My text field :

@IBOutlet weak var txtInput: UITextField!

ADD button :

@IBAction func btnAdd(_ sender: UIButton) {
        let testScore = txtInput.text
        scoreDisplay.text = testScore
    }

The output/display of number should be , example , 40, 23.6, 98.2, 74.4 What i get is just the replacement of numbers.

2 Answers 2

1

You should append using += instead of = (which replace the whole value)

scoreDisplay.text += ", \(testScore)"
scoreDisplay.text = scoreDisplay.trimmingCharacters(in: CharacterSet(charactersIn: " ,"))

Trimming is used to remove the first , in your string.

Edit

Since text property of a UILabel is Optional<String> you actually cannot use +=. So:

scoreDisplay.text = scoreDisplay.text ?? "" + ", \(testScore)"
scoreDisplay.text = scoreDisplay.text?.trimmingCharacters(in: CharacterSet(charactersIn: " ,"))
Sign up to request clarification or add additional context in comments.

2 Comments

I did that an got an error " Expression type '@lvalue String?' is ambiguous without more context " . Not sure what it means and how to fix it?
@KHY it's because text property is optional, check my edit
0

EDIT: I found the problem. Both scoreDisplay.text and testScore.text are optional, so you must make sure they have a value.

@IBAction func btnAdd(_ sender: UIButton) {
   guard let testScore = txtInput.text, let scoreDisplay = scoreDisplay.text else  { return }
   scoreDisplay.text = scoreDisplay + testScore
  } 

Also, if you want to add ", " in the middle of each number:

@IBAction func btnAdd(_ sender: UIButton) {
   guard let testScore = txtInput.text, let scoreDisplay = scoreDisplay.text else  { return }
   scoreDisplay.text = "\(scoreDisplay), \(testScore)"
  }

PD: The = only remplace your string with other, the += append a new string.

5 Comments

It gives an error " Expression type '@lvalue String?' is ambiguous without more context " Do u know how i can add the input number from tex field into an array at the same time?
What type of data is your array? [String] , [Int], [Double] ... ?
Data type is Double
Try this in the func where you want to add the input into an array: if let testScore = Double(txtInput.text) { theNameOfYourArray.append(testScore) }
You ask if txtInput.text can converse into a double, and if is possible, append into your array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.