The difference between nil, Nil, NULL, and NSNull in Swift (2024)

  • Report this article

Deya Eldeen Elkhawaldeh The difference between nil, Nil, NULL, and NSNull in Swift (1)

Deya Eldeen Elkhawaldeh

iOS Engineer SDE III @ Expedia Group

Published Jan 7, 2022

+ Follow

As a programmer, sometimes you will need to define “nothingness”… 🧐

Data stores can have a value, or simply be nothing, thisnothingcomes in different flavors (nil, Nil, Null, NSNull) which all came to be called “null”.

The difference between nil, Nil, NULL, and NSNull in Swift (2)

In Swift, you will not be able to deal directly with NULL and Nil,

say you have this code in Objective C

// Machine.h#import <Foundation/Foundation.h>@interface Machine : NSObject@property (strong, nonatomic) id serialNumber;- (void) summary;@end// Machine.m#import <Foundation/Foundation.h>#import "Machine.h"@implementation Machine- (void) summary { NSLog(@"Initializing Machine"); NSString *label1 = @"label1"; NSString *label2 = nil; NSString *label3 = [NSNull null]; NSString *label4 = Nil; NSString *label5 = NULL; NSLog(@"Label1 = %@",label1); NSLog(@"Label2 = %@",label2); NSLog(@"Label3 = %@",label3); NSLog(@"Label4 = %@",label4); NSLog(@"Label4 = %@",label5);}@end 

after preparing the bridging header, you will be able to create Machine Objects

let OC_Machine = Machine()OC_Machine.summary()OC_Machine.serialNumber = 21OC_Machine.serialNumber = nilOC_Machine.serialNumber = NSNull()//OC_Machine.serialNumber = Nil//OC_Machine.serialNumber = Nullprint(OC_Machine.serialNumber) 

As you can see, the commented lines will not compile in swift, but should be running ok in Objective C, you will see this output

Label1 = label1

Label2 = (null)

Label3 = <null>

Label4 = (null)

Label4 = (null)

Optional(<null>)

[NSNull null] is a wrapper for nil

nilis defined as :#define nil NULLand is Objective C equivalent for CNULL

Nil is for object pointers, NULL is for non pointers, Null and Nil both defined to be equal to the value zero.

NULLis avoid *,nilis anid, andNilis a Class pointer,NULLis used fornon-object pointer(like a C pointer) in Objective-C. Likenil,NULLgot no value nor address (used to check if a struct is empty).

Keep in mind these quotes:

In Objective-C: nil is a pointer to a non-existent object.
In Swift: nil is not a pointer, it’s the absence of a value of a certain type.
NULL and nil are equal to each other, but nil is an object value while NULL is a generic pointer value ((void*)0, to be specific).[NSNull null]is an object that’s meant to stand in for nil in situations where nil isn’t allowed. For example, you can’t have a nil value in an NSArray. So if you need to represent a “nil”, you can use[NSNull null].
Like
Comment

13

1 Comment

Deya Eldeen Elkhawaldeh

iOS Engineer SDE III @ Expedia Group

2y

  • Report this comment

💡: I have updated this article, it had some alignment missed up.

Like Reply

1Reaction 2Reactions

To view or add a comment, sign in

More articles by this author

No more previous content

  • Easily Tiling Images in Xcode's interface builder, without code ! Feb 23, 2022
  • Why my 0.3 MB image occupies around 10 MB on RAM? Feb 22, 2022
  • Never Use Floating-Point / Double Data types for Monetary Calculations! Feb 12, 2022
  • Swift Style Guides (a brief look) Jan 7, 2022
  • The Difference Between Void and () in Swift Jan 7, 2022

No more next content

See all

Sign in

Stay updated on your professional world

Sign in

By clicking Continue to join or sign in, you agree to LinkedIn’s User Agreement, Privacy Policy, and Cookie Policy.

New to LinkedIn? Join now

Insights from the community

  • System Development How can you implement a linked list data structure in C++?
  • STL What are the benefits and drawbacks of using stream manipulators in C++?
  • Shell Scripting What are some advantages and disadvantages of using set -e, -u, and -x options in shell scripts?
  • Algorithms How can you efficiently resize a dynamic array?
  • Programming How can you implement a thread-safe observer pattern?
  • Computer Science How can a self-balancing binary search tree improve your code performance?
  • Algorithms What is the most effective way to test the efficiency of a new data structure?
  • Computer Science How can you use a disjoint-set data structure to generate a maze?
  • Multithreading What are some examples of lock-free and wait-free data structures and algorithms in multithreading?

Others also viewed

  • Conversion of Algol Code into C# Traditional Switch Statement Shelvin Datt 10mo
  • C++ 11 Lambda Functions Gamindu Udayanga 7y
  • How to get the real type of object in C++? Konstantin Nikitin 5y
  • C++23: More Small Pearls Rainer Grimm 9mo
  • The Power of Transformation in C++: An Exploration of std::transform" Salik Tariq 11mo
  • Bit Manipulation Saurav Kumar 1y
  • C++ Core Guidelines: Comparison, Swap and Hash Rainer Grimm 6y
  • Dynamic Store Fields Type Info with C++ Ruipeng Wang 1mo
  • C++ string views from high performance to undefined behaviour Martin Ayvazyan 1y
  • HackerRank, Algorithms with Ruby. #2 Warmup - Simple Array Sum Sungmo Yeon 7y

Explore topics

  • Sales
  • Marketing
  • Business Administration
  • HR Management
  • Content Management
  • Engineering
  • Soft Skills
  • See All
The difference between nil, Nil, NULL, and NSNull in Swift (2024)

FAQs

The difference between nil, Nil, NULL, and NSNull in Swift? ›

In Swift: nil is not a pointer, it's the absence of a value of a certain type. NULL and nil are equal to each other, but nil is an object value while NULL is a generic pointer value ((void*)0, to be specific). [NSNull null] is an object that's meant to stand in for nil in situations where nil isn't allowed.

What is the difference between nil and NULL? ›

They mean different things. Null is an adjective meaning 'invalid' or 'nonexistent. ' Nil is a noun meaning 'nothing' or 'nought'.

What is NSNull? ›

A singleton object used to represent null values in collection objects that don't allow nil values.

What is the meaning of nil in Swift? ›

In Swift, nil is not a pointer but the absence of a value of a certain type. Optionals of any type can be set to nil, not just object types. NULL and Nil have no equivalent in Swift. I will add more examples and other significant differences to get more clarity if I get anything related to this.

What is the difference between nil and none? ›

There is no difference between . nil and . none , if . nil == .

What is the difference between NSNull NULL and NIL? ›

[NSNull null] is an object that's meant to stand in for nil in situations where nil isn't allowed. For example, you can't have a nil value in an NSArray. So if you need to represent a “nil”, you can use [NSNull null].

What is the difference between NIL and NIL in Swift? ›

In terms of the underlying implementation, nil in Objective-C is a predefined macro that represents a pointer to a null object, while nil in Swift is a special type that represents the case of an optional type (enumerated type) that is missing a value.

How to check nil in Swift? ›

Nil-coalescing operator

In Swift, you can also use nil-coalescing operator to check whether a optional contains a value or not. It is defined as (a ?? b) . It unwraps an optional a and returns it if it contains a value, or returns a default value b if a is nil.

What is the difference between null and nil in Objective-C? ›

Objective-C nothing is nil. nil is an object pointer to nothing. Although semantically distinct from NULL, they are technically equivalent to one another. On the framework level, Foundation defines NSNull, which defines a class method +null, which defines the singleton NSNull object.

How to check nil in Objective-C? ›

In short, in Objective-C, when a dictionary does not have a given key, it will point to nil . When the dictionary does have the key, but said key is null, it will be NSNull .

How to handle nil values in Swift? ›

Swift provides a range of techniques to handle nil values, such as optional chaining, forced unwrapping, and the guard statement. Optional chaining is a concise way to work with optionals and allows you to safely access properties, methods, and subscripts of an optional that might be nil.

What does nil mean? ›

In the simplest of terms, Name, Image and Likeness (NIL) is a term that describes the means through which college athletes are allowed to receive financial compensation. NIL refers to the use of an athlete's name, image, and likeness through marketing and promotional endeavors.

How to handle null in Swift? ›

If the value of "id" is null in our JSON, parseRepository returns nil . If "id" is a string, parseRepository returns nil . If "id" is an object, parseRepository returns nil . And if "id" is an integer, we'll continue parsing our JSON and eventually return a Repository if all goes well.

What is the difference between Null and Nul? ›

nul and null are two different things. null is a pointer to address 0, while nul is the name of the character with value 0. That distinction is sometimes elided (people often say "null-terminated" rather than "nul-terminated"), but that's the distinction the API names are based on.

What is the difference between none and Null? ›

None means Nothing, Nothing is a concept that describes the absence of anything at all. Nothing is sometimes confused with Null, but they are very different concepts because Nothing means absence of anything, while Null means unknown (you do not know if there is a thing or not).

What is a NIL answer? ›

a reply of zero to a request for a quantified reply.

Why does go use NIL instead of NULL? ›

About nil. nil is a predefined identifier in Go that represents zero values of many types. nil is usually mistaken as null (or NULL) in other languages, but they are different. Note: nil can be used without declaring it.

Why do people say NULL instead of zero? ›

Null. In certain contexts, zero and nothing are interchangeable, as is "null". However, in mathematics and many scientific disciplines, a distinction is made (see null). The number 0 is represented by zero while null is a representation of an empty set {}.

Is 0 and NULL the same thing? ›

The answer to that is rather simple: a NULL means that there is no value, we're looking at a blank/empty cell, and 0 means the value itself is 0. Considering there is a difference between NULL and 0, the way Tableau treats these two values therefore is different as well.

Does NULL mean nothing? ›

Null means having no value; in other words null is zero, like if you put so little sugar in your coffee that it's practically null. Null also means invalid, or having no binding force. From the Latin nullus, meaning "not any," poor, powerless null is not actually there at all. Or if it was, it's gone now.

Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6051

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.