What is === and !== in Swift ? Is it similar like in JS?

395 Views Asked by At

I am trying to convert a js code into swift (new in both). Where I found that in js they have used === for comparing json values with string.

Can someone help to me to find the meaning of === in swift (3.0) also does it do the same operation like JS? Usages I found in js -

                    try {
                        var rsltjsn = JSON.parse(responseData1[0]);
                        console.log("valid json")
                    } catch (e) {


                    for (var k in rsltjsn) {
                        console.log(k)
                        if (k === "access_token") {  //<-what it means
....}
..}
1

There are 1 best solutions below

0
On

In js === means exactly equal, so the number 2 and the string '2' are == but not ===.

In swift however, the === is used to compare classes. It means that the two classes are exactly the same. This is if you want to check that two instances actually are referring to the same class of the same value.

In reality, you need to use === in js for your comparisons, but in swift == is what you most commonly need