3

I'm coming from a Java/Android background where we use NULL. Now I am doing Swift/iOS and I am confused as to what Swift's nil means.

Can I use it like NULL in Java? Does it act the exact same way or is there something different about its usage?

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Micro
  • 10,303
  • 14
  • 82
  • 120

3 Answers3

5

You can think of 'null' and 'nil' the same. Whether the language includes optionals is a separate concern.

Objective-C, has 'nil', but does not have in-built optionals while Swift does. Similarly, Java has 'null', but not have implicit optionals, while several JVM languages such as Kotlin, Scala and Ceylon do, and did so before Swift. &nbsp

Here's an article that compares about null, nil and optionals in Kotlin, Scala and Swift: http://codemonkeyism.com/comparing-optionals-and-null-in-swift-scala-ceylon-and-kotlin/

Incidentally, for Android development you may want to investigate Kotlin and the associated Anko library from Jetbrains.

Andor Németh
  • 451
  • 6
  • 12
  • 27
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
1

Swift does not currently support a null coalescing operator.

Swift nil explanation

nil means "no value". Non-optional variables cannot be assigned nil even if they're classes, so it's explicitly not a null pointer and not similar to one.

I can recommend you to read more about optionals in Swift.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • 1
    No, actually, it's not equivalent. Definitely read the optionals link. – Jack Lawrence Dec 05 '15 at 00:11
  • @JackLawrence how is it not equivalent. You have peaked my interest. I think the only difference lies in what it can be assigned to, but it has the same semantic meaning. And if everything in Java was like original obj-c and considered an implicitly unwrapped optional, they'd be the same. – sylvanaar Dec 05 '15 at 00:19
  • You´re right read more about it and learned it now. @sylvanaar check the updated answer. – Rashwan L Dec 05 '15 at 00:20
  • I suppose it only has meaning in relation to optionals. I guess that makes it different enough. – sylvanaar Dec 05 '15 at 00:28
  • Exactly. +1'd the edited answer. `Optional` is an enum and `nil` is just a faster way of typing `Optional.None`. – Jack Lawrence Dec 05 '15 at 00:32
  • 2
    I think you're arguing over semantics. `null` in Java is a type, which can be cast to any non-primitive reference type; `nil` can also be cast to any reference type, provided it is declared an optional. The primary difference is more about the setup of the variable, rather than the null/nil value. By-and-large the null/nil are mostly the same (a lack of value, that isn't 0), but the rules in how they can be assigned are slightly different between Java and Swift. – vol7ron Dec 05 '15 at 00:39
  • @vol7ron "By-and-large the null/nil are mostly the same (a lack of value, that isn't 0)" is the kind of answer I was looking for. I just really need to know if I can just think of null/nil the same as I go forward learning swift and eventually am making the same app for android/ios. – Micro Dec 05 '15 at 01:24
  • @MicroR Swift does not currently support a null coalescing operator. Check the updated answer. – Rashwan L Dec 05 '15 at 08:28
  • The main difference between `null` and `nil` is that trying to, say, call methods on `nil` is perfectly legal (if you mark them as optional), while doing the same in Java results in a null pointer exception. – BallpointBen Dec 05 '15 at 09:16
1

Disclosure: I don't know a whole terrible lot about Java, so my answer is coming from a C++/Objective-C/Swift perspective.

Matt Thompson of NSHipster has a great post about it and how it relates to Objective-C vs C. You can find it here.

The answer basically boils down to this: you should consider it the same.

  • 0 is how you represent nothing for a primitive value
  • NULL is how you represent nothing for literal for C pointers
  • nil is how you represent nothing for literal Objective-C/Swift objects
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
dokun1
  • 2,120
  • 19
  • 37