user4882614
user4882614

Reputation:

Error in Swift, Cannot assign to a literal value

I've been trying to make an if statment in swift that checks if a label equals the same text. Like this,

if splitLabel.text = 1.0 = true {
        var tipTotal = percentage * Double((billAmount)!)

but I get the error Cannot assign to a literal value

This is in Swift with Xcode 7

Upvotes: 1

Views: 964

Answers (2)

zaph
zaph

Reputation: 112857

There is some guesswork since there are two assignment operators (=) in the statement.

  1. You can't compare a string (splitLabel.text) to a number (1.0). They are not the same types so can not be compared.
  2. The comparison operator is ==, not =, the latter is the assignment operator.
  3. There are two assignment operators (=) in the statement.

Probably you want something like:

if splitLabel.text == "1.0" {

Upvotes: 4

Eluss
Eluss

Reputation: 522

You try to assign "true", I think you made a typo there

Use this code instead

if splitLabel.text ==  "1.0" {
        var tipTotal = percentage * Double((billAmount)!)
}

Upvotes: 0

Related Questions