Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

njharris97

macrumors member
Original poster
Hi,

I have just started to learn a programming language from scratch, and am working with Swift. Can anyone solve this problem.

Code:
func apple123(){

    var apple = "1"

    if apple = "2" {              - here I get the error cannot assign to immutable value of type 'string'

        println("True")

    }

    else {

        println("FALSE")

    }

}

Thanks in advance
 
Last edited by a moderator:
What do you intend this line of code to do:
Code:
    if apple = "2" {              - here I get the error cannot assign to immutable value of type 'string'
Is it to compare a string with another string ("2")? Or is it to assign a string ("2") to a variable?

If your intent is comparison, then you need to review the comparison operators and look very carefully at what code you wrote.

If your intent is assignment, then you should rethink the use of if/else.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.