- Report this article
Deya Eldeen Elkhawaldeh
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”.
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
Celebrate
Support
Love
Insightful
Funny
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.
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
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