Value Type and Reference Type in Swift

·

3 min read

Cover Image for Value Type and Reference Type in Swift

Call by value and Call by reference is a fundamental topic in a programming language. It tells you how your data will be manipulated in scopes.

Value Types

Value types are Pass by Value. This means that copies of their value are used when passed to functions or assigned to other variables.

This behavior is called Pass by Value because you are passing a copy of the value to the function. Primitive types (e.g., numbers, strings, booleans) are immutable in Swift, this means their values cannot be changed after creation.

These values are like locked boxes – you can't change what's inside. If you modify the value inside a function, it won't change the original value outside the function because you're working with a separate copy. When you modify a primitive type inside a function, you're working with a new copy of the value, leaving the original value unaffected.

  1. Int- Represents signed integers.

  2. UInt- Represents unsigned integers.

  3. Specific sizes like Int8, Int16, Int32, and Int64 are also available.

  4. Double- Represents double-precision floating-point numbers.

  5. Float- Represents single-precision floating-point numbers.

  6. Bool- Represents Boolean values (true or false).

  7. Character- Represents a single Unicode character.

  8. String- Represents a collection of characters.

  9. Array- Represents an ordered collection of elements of the same type.

  10. Dictionary- Represents an unordered collection of key-value pairs.

  11. Set- Represents an unordered collection of unique elements.

  12. Range- It is a fundamental data type used to represent a range of values. Ranges are often used for iterating over a sequence of values, accessing elements in a collection, or performing various other operations where a span of values is needed.

  13. Tuples - Tuples are value types that allow you to group multiple values together. They are commonly used for returning multiple values from functions.

  14. Structs - Structs are value types that can encapsulate properties and methods. They are typically used for small, self-contained pieces of data.

  15. Enums - Enums can also be value types in Swift and are often used to represent a group of related values or states.

Reference Types

Reference Types are Passed by Reference. The primary reference type is classes. When you pass a reference type (a class) as an argument, you're passing a reference to the same object in memory. Changes inside the function affect the original object because both the function and the calling code reference the same memory location.

  1. Classes: Classes are reference types and are used to create objects with properties and methods. They have reference semantics, meaning when you pass a class instance as an argument, you're passing a reference to the same object in memory.

  2. Functions: Functions are reference types in Swift. You can assign functions to variables, pass them as arguments, and return them from other functions.

  3. Closures: Closures, like functions, are reference types. They capture and store references to variables and constants from the surrounding context.

  4. Objects and Instances: Any custom objects or instances created from classes are reference types. This includes instances of custom data types you create.

More Swift Tutorials

Follow me for more such content