2

I cannot simply understand how this can happen, must have misunderstood some basic swift syntax. As you can see on my image, the debugger steps into the if statement (the green line), but "strd" and "elementName" is not the same. How can this happen? And it happens all the time. As I have understood you are supposed to use '==' when comparing strings in swift.

enter image description here

EDIT: The solution nimit proposes does acually work, but it doesnt answer my question. What have I done wrong and how do I avoid getting this error in the furure?

Zeezer
  • 1,503
  • 2
  • 18
  • 33

1 Answers1

1

Its working fine if we put static string

var str1 : String? = "asdd"

var strd = "device"
var elementname = "major"


if str1 != nil && strd == elementname
{
    println("You are in if condition")
}
else
{
    println("There is no condition mactched")
}

Just do one thing, take on variable and stored elementname into it and try.

Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
  • Assigning elementname to a 'var' actually solved my problem ('let' didnt work). Why is this so? This doesnt seem like a typical behaviour, rather like a bug... – Zeezer Nov 04 '14 at 12:28
  • http://stackoverflow.com/questions/24002092/what-is-the-difference-between-let-and-var-in-swift you can check answer in this link. As of know, your issue is solved ? – Nirmalsinh Rathod Nov 04 '14 at 12:37
  • I know the difference between 'let' and 'var', thats why it is so strange that only 'var' worked. Even though it now works, I still would like an explanation what I did wrong? – Zeezer Nov 04 '14 at 13:10