Swift: The Difference Between Void and ()
5 (1)

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

Void is a data type that is common across a lot of programming languages, in Swift’s standard library, it’s simply an empty tuple, it’s used for for functions that return nothing, when defining a function, if you don’t specify a return type, you get a function that return Void, this is how it’s defined in standard library.

public typealias Void = ()

You use Void to declare the type of a function, method, or closure, Keep in mind 🤓
that () can mean two things:

() can be a type – the empty tuple type, which is the same as Void.
() can be a value – an empty tuple, which is the same as Void().

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

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;


// 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);



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

let OC_Machine = Machine()
OC_Machine.serialNumber = 21
OC_Machine.serialNumber = nil
OC_Machine.serialNumber = NSNull()
//OC_Machine.serialNumber = Nil
//OC_Machine.serialNumber = Null

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)

[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]?