Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null / nil in swift language

Tags:

ios

swift

How would we define the following in swift programming language :

  • null
  • nil
  • Nil
  • [NSNull null]

In other words, what would be the swift equivalent of each of these objective c terms. Besides, would also like to know whether any specific use cases exist for non objective c types like structs and enums. Thanks in advance.

like image 772
Mehul Parmar Avatar asked Jun 04 '14 17:06

Mehul Parmar


1 Answers

Regarding equivalents:

  • NULL has no equivalent in Swift.
  • nil is also called nil in Swift
  • Nil has no equivalent in Swift
  • [NSNull null] can be accessed in Swift as NSNull()

Note: These are my guesses based on reading and play. Corrections welcome.

But nil/NULL handling in Swift is very different from Objective C. It looks designed to enforce safety and care. Read up on optionals in the manual. Generally speaking a variable can't be NULL at all and when you need to represent the "absence of a value" you do so declaratively.

like image 58
gwcoffey Avatar answered Oct 20 '22 00:10

gwcoffey