Junior
What is the difference between NIL and NaN in iOS development?
sobes.tech AI
Answer from AI
nil is used to indicate the absence of a value for reference types (class objects). In Swift, it is also used for optional types. NaN ("Not a Number") is a special floating-point value used to represent invalid numerical results of operations, such as dividing zero by zero or taking the square root of a negative number.
// Example of using nil for an optional type in Swift
var optionalString: String? = nil // Reference type variable, optional
print("Optional string is: \(optionalString ?? "nil")")
// Example of using NaN in Swift
let result = 0.0 / 0.0
if result.isNaN {
print("Result is NaN")
}
| Term | Usage | Data Types | Value |
|---|---|---|---|
nil |
Absence of object reference, absence of value in an optional | Reference types, optional types (classes, structures, enumerations) | Indicates that a variable or constant has no value |
NaN |
Result of invalid operations with floating-point numbers | Floating-point numeric types (Float, Double) | Indicates that the numeric value is not a valid number |