Swift: the difference between nil, Nil, NULL, NSNull
5 (1)

Click to rate this post!
[Total: 1 Average: 5]

As a programmer, sometimes you will need to define “nothingness”… 🧐
Data stores can have a value, or simply be nothing, this nothing comes in different flavors (nil, Nil, Null, NSNull) which all came to be called “null”.

Meaning
NULLliteral null value for C pointers
nilliteral null value for Objective-C objects
Nilliteral null value for Objective-C classes
NSNullsingelton object used to represent null
null types


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 = 21
OC_Machine.serialNumber = nil
OC_Machine.serialNumber = NSNull()
//OC_Machine.serialNumber = Nil
//OC_Machine.serialNumber = Null
print(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

nil is defined as : #define nil NULL and is Objective C equivalent for C NULL

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

NULL is a void *nil is an id, and Nil is a Class pointer, NULL is used for non-object pointer (like a C pointer) in Objective-C. Like nil , NULL got no value nor address (used to check if a struct is empty).

keep in mind:

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].

Swift: Any/ AnyObject/ AnyHashable Differences
5 (1)

Click to rate this post!
[Total: 1 Average: 5]

Although in normal cases, you should be specific in defining data types, swift offers Any, AnyObject, and AnyHashable.

Any can represent an instance of any type, including functions, instance of a class, struct, or enum, it’s more general than AnyObject, where AnyObject is a protocol all classes indirectly conform to.

AnyObject is useful when using Objetive-C / Swift, some parts of Objective-C use this protocol to enhance compatibility with Swift, it’s equivelant to ‘id’ in Objective-C.

so, when to use Any / AnyObject ?
say you have a dictionary..

let anyStuff: [Any] = [1,”z”,3,[]]

If your data will be used only in Swift code, then you should use Any because your types (Int, Double, Float, String, Array, and Dictionary) are not objects.

If it will be passed to Objective-C code that expect an NSDictionary, then go with AnyObject.

AnyHashable is a super-type that is defined as a struct, it was introduced in Swift 3 standard library, it’s used to bring untyped sets and dictionaries from Objective-C to Swift.

public struct Notification : ReferenceConvertible, Equatable, Hashable {
    /// Storage for values or objects related to this notification.
    public var userInfo: [AnyHashable : Any]?
}

Swift Basic Data Types & Type Inference
5 (1)

Click to rate this post!
[Total: 1 Average: 5]

Swift is strongly typed, data should either be explicitly assigned or inferred, the main basic data types that come with swift are

TypeDescription
Charactera 16-bit Unicode character like “a” or “/”
Stringrepresents textual data like “Hello”
Floatrepresents 32-bit floating-point number
Doublerepresents 64-bit floating-point number
Boollogical value: true or false
Tuplesgroups multiple values in single value
Intinteger, a whole number
UIntunsigned integer, a whole number
Int81 byte integer
Int324 bytes integer
Int648 bytes integer
UInt81 byte unsigned integer
UInt324 bytes unsigned integer
UInt648 bytes unsigned integer


On a 32-bit device, Int has the size of Int32, on 64-bit devices, Int has the size of Int64, same goes for UInt.

Swift has a feature where it can infer the type of the data directly, for example the variable level here is infered to be an Int

var level = 12

you can explicitly determine the type directly

var level: Int = 17

Extra Tip: A CGFloat holds either 32-bits of data or 64-bits of data depending on the CPU Architecture.