Null/Nil in Swift Language

null / nil in swift language

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.

Java null vs Swift nil

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.  

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.

How to Nil a object in Swift

From Apple Documentation:

nil cannot be used with nonoptional constants and variables. If a
constant or variable in your code needs to work with the absence of a
value under certain conditions, always declare it as an optional value
of the appropriate type

also very importante to notice:

Swift’s nil is not the same as nil in Objective-C. In Objective-C, nil
is a pointer to a nonexistent object. In Swift, nil is not a
pointer—it is the absence of a value of a certain type. Optionals of
any type can be set to nil, not just object types

I hope that helps you!

Does anybody know the rationale behind (nil 0) == true and (nil = 0) == true in Swift?

Optionals are comparable, so they can be sorted, for example. The rules are very simple:

  1. Any two optionals that are nil compare equal.
  2. If one of the optionals is nil and the other is not, nil is less than non-nil.
  3. If both optionals are not nil, then the unwrapped values are compared.

As a consequence, nil equals nil, and nil is less than any non-nil optional.

It has nothing to do with the value 0 that you assigned. Assign -1000, or +100, or whatever you like, and you get the same result.

NULL parameter in Swift

Your Objective-C method has nullable pointers as parameters,
in Swift 3 that would be an optional UnsafeMutablePointer:

func getRect(aRectRef: UnsafeMutablePointer<CGRect>?, bRectRef: UnsafeMutablePointer<CGRect>?) {
if let aRectPtr = aRectRef {
aRectPtr.pointee = CGRect(x: 1, y: 2, width: 3, height: 4)
}
if let bRectPtr = bRectRef {
bRectPtr.pointee = CGRect(x: 5, y: 6, width: 7, height: 8)
}
}

var rect = CGRect.zero
getRect(aRectRef: &rect, bRectRef: nil)
print(rect) // (1.0, 2.0, 3.0, 4.0)

So you can pass nil as an argument. What you can not do
(in contrast to Objective-C) is to pass the address of an uninitialized variable, rect must be initialized here.

The same can be written more compactly as

func getRect(aRectRef: UnsafeMutablePointer<CGRect>?, bRectRef: UnsafeMutablePointer<CGRect>?) {

aRectRef.map { $0.pointee = CGRect(x: 1, y: 2, width: 3, height: 4) }
bRectRef.map { $0.pointee = CGRect(x: 5, y: 6, width: 7, height: 8) }
}

Are nil and Optional T .None the same thing in Swift?

If you don’t provide an initial value when you declare an optional
variable or property, its value automatically defaults to nil.

They have same value, both are nil.

Actually Optional<T>.None is a polymorphic primitive value, and nil is a constant having this value. Optional<T> is a polymorphic type. The type of nil is Optional<T>. That's why you cannot assign nil to anything else but an Optional<T>. For the same reason you cannot assign true to anything but a Bool.

 

For now, according to the documentation you can not use nil for any custom and arbitrary type but optionals.

nil cannot be used with non-optional constants and variables. If a
constant or variable in your code needs to be able to cope with the
absence of a value under certain conditions, always declare it as an
optional value of the appropriate type.



Related Topics



Leave a reply



Submit