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
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().
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”.
literal null value for C pointers
literal null value for Objective-C objects
literal null value for Objective-C classes
singelton object used to represent null
In Swift, you will not be able to deal directly with NULL and Nil, say you have this code in Objective C
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].