Never Use Floating-Point / Double Data types for Money Calculations!
5 (1)

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

Floating point values, or even (Double precision floating point format), should be avoided when using a currency amount with fractions (like Dollars and cents), in its nature, it cannot be stored exactly as is in memory.

Say we want to store 0.1 dollars, any floating-point data type can not store it as is, it get’s stored as an approximation (0.10000000149….).

When doing a series of math operations, some problem can rise, that is called (loss of significance), the errors can be amplified and cause trouble 🧐.

the solution is simple, use NSNumber

let myBalance = 12.333
let decimal: Decimal = NSNumber(floatLiteral: 12.333).decimalValue
let result = decimal / 3

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