Glossary Of Swift Common Terms

Introduction

 
In Swift programming, there is so much confusing syntax and functionality, so here I will give a brief introduction for all common terms used in Swift language. 
 
@autoclosure
 
An attribute attached to function parameters that are closures, which asks Swift to silently wrap any code using it in a closure rather than requiring users to do it by hand. This is used rarely, but it's important in the assert() function.
 
@available
 
An attribute attached to types or functions that mark them as being available or unavailable to specific versions of Swift or operating system. This Xcode feature is the ability to have Xcode automatically check API availability for you, which means to refuse to run code that is not available on the minimum iOS version you support. 
 
Swift has #available, which lets you state that a certain block of code should only execute on the specific version.
 
Example
 
The below code checks whether the user has iOS 13 or later on the device,
  1. if #available(iOS 13, *) {  
  2.     // use SwiftUI framework  
  3. else {  
  4.     // show alert   
  5. }  
@discardableResult
 
An attribute attached to methods that return to value, marking the return value as safe to ignore if the caller wants to. When this is not used, Swift will show a warning If you don't do something with the function's return value.
 
@dynamicCallable
 
An attribute attached to types to mark them as being directly callable, primarily so that Swift can interact more easily with a dynamic language such as Python.
 
@dynamicMemberLookup
 
An attribute attached to types to mark them as being able to handle undefined properties using special methods, primarily so that Swift can interact more easily with dynamic languages such as Python.
 
@escaping
 
An attribute attached to function parameters that are closures, which tells Swift the closure will be used after the function has returned. This will in turn cause Swift to store the closure safely so it doesn't get destroyed prematurely.
 
Example
  1. var complitionHandler: ((Int)->Void)?  
  2.     func getSumOf(array:[Int], handler: @escaping ((Int)->Void)) {  
  3.        //here I'm taking for loop just for example, in real case it'll be something else like API call  
  4.         var sum: Int = 0  
  5.         for value in array {  
  6.             sum += value  
  7.         }  
  8.         self.complitionHandler = handler  
  9.     }  
  10.     func doSomething() {  
  11.         self.getSumOf(array: [16,756,442,6,23]) { [weak self](sum) in  
  12.             print(sum)  
  13.             //finishing the execution  
  14.         }  
  15.     }  
  16. //Here we are storing the closure for future use.  
  17. //It will print the sum of all the passed numbers.  
@objc
 
An attribute used to mark methods and properties that must be accessible to Objective-C code. Swift doesn't make its code accessible to Objective-C by default to avoid making the code larger than it needs to be.
 
@objcMembers
 
An Attribute used to mark classes where all properties and methods must be accessible to Objective-C code. Swift doesn't make code accessible to Objective-C by default to avoid making the code larger than it needs to be.
 
@unknown
 
An Attribute attached to the default case of switch blocks that allows code to handle enum cases that may be added at some point in the future, without breaking source compatibility.
 

ABI

 
ABI stands for Application Binary Interface. At runtime, Swift program binaries interact with other libraries through an ABI. It defines many low-level details for binary entities like how to call functions, how their data is represented in memory, where metadata is and how to access it.
 
Associated type
 
A missing type in a protocol that must be specified by whatever type is conforming to the protocol. Associated types allow us to have flexibility when adding conformances, we can say that to conform to our protocol you must have an array of items, but we don't care what the data type of those items is. Associated types are written as one in Swift is associtedtype. Associated types are a powerful way of making protocols generic.
 
An associated type gives a placeholder name to type that is used as a part of the protocol. The actual type to use that is not specified until the protocol is adopted. 
 
Example
 
A protocol called Container, which declare an associated type called item,
  1. protocol Container {  
  2.     associatedtype Item  
  3.     mutating func append(_ item: Item)  
  4.     var count: Int { get }  
  5.     subscript(i: Int) -> Item { get }  
  6. }  
Above protocol example doesn't specify how the items in the container should be stored or what type they're allowed to be.  
 
Associated value
 
A value that has been added to an enum case to provide some extra meaning e.g. you might have an enum case saying the weather is windy, then add an associated value saying how windy.
 
Block
 
Block means any chunk of code that starts with {and ends with }("a code of block") but "block" is also the Objective-C name for closures.
 
Capturing values
 
The name for the process of closures keeping a reference to values that are used inside the closure but were created outside. This is different from copying the closure refers to the original values, not it's own copies, so if the original values change then the closure's values change too.
 
CaseIterable
 
A Swift protocol that can be applied to enums. If the enum has cases with no associated values, the compiler will generate an all cases array that lets you loop over the cases in the enum.
 
CGFloat
 
A floating-point number that may be equivalent to a Double or Floats depending on the platform.
 
Class
 
A custom data type that can have one or more properties and one or more methods unlike structs, classes are reference types.
 
Class inheritance
 
The ability for one class to build on another, inheriting all its methods and properties. some languages allow one class to inherit from multiple parents, Swift does not.
 
Closure
 
An anonymous function that automatically keeps a reference to any values it uses that were declared outside the function.
 
Example
  1. // Closure take no parameter and return nothing  
  2. let sayHello: () -> Void = {  
  3.     print("Hello")  
  4. }  
  5.   
  6. sayHello()  
  7.   
  8. // Closure take one parameter and return 1 parameter  
  9. let value: (Int) -> Int = { (value1) in  
  10.     return value1  
  11. }  
  12.   
  13. print(value(5))  
  14.   
  15. // Closure take two parameter and return 1 parameter  
  16. let add: (Int, Int) -> Int = { (value1, value2) in  
  17.     return value1 + value2  
  18. }  
  19. print(add(5, 4))  
  20.   
  21. // Closure take two parameter and return String parameter  
  22. let addValues: (Int, Int) -> String = { (value1, value2) -> String in  
  23.     return String("Sum is: \(value1 + value2)")  
  24. }  
  25. print(addValues(5, 4))  
Codable
 
A protocol that allows easy conversion between a struct or a class and JSON or XML. Codable Actually a type alias that combines two protocols - Encodable and Decodable into one.
 
Example
  1. struct User: Codable {  
  2.     var name: String  
  3.     var age: Int  
  4. }  
Above code adding that single protocol conformance, we are now able to encode a user instance into JSON Data by using a JSONEncoder 
  1. do {  
  2.     let user = User(name: "Pravesh Dubey", age: 31)  
  3.     let encoder = JSONEncoder()  
  4.     let data = try encoder.encode(user)  
  5. catch {  
  6.     print("Whoops, an error occured: \(error)")  
  7. }  
Collection
 
A Swift protocol that is used by sequences types you can traverse multiple times without destroying them such as arrays and dictionaries.
 
Comparable
 
A common Swift protocol that says conforming types can be placed into an order using <.
 
Computed properties
 
Any property that doesn't have a storage area for its value, and instead is calculated each time the property is accessed by running some code.
 
Defer
 
A keyword that allows us to schedule work for when the current scope is exited.
 
Example :
  1. func printStringNumbers() {  
  2.     defer { print("1") }  
  3.     defer { print("2") }  
  4.     defer { print("3") }  
  5.   
  6.     print("4")  
  7. }  
  8. /// Prints 4, 3, 2, 1  
Deinitializer
 
A special method that is called when an instance of a class is being destroyed. these may not accept parameters and don't exist on structs.
 
Enum
 
An enum defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.
 
Example
  1. enum CompassPoint {  
  2.     case north  
  3.     case south  
  4.     case east  
  5.     case west  
  6. }  
Extension
 
The extension adds new functionality to an existing class, structure, enumeration or protocol type. This includes the ability to extend types for which you do not have access to the original source code.
 
Fallthrough
 
A keyword used in switch blocks means "carry on executing the case immediately following this one".
 
Failable initializer
 
An initializer that returns an optional value, because initialization might have failed for some reason. These are written in init?() and init!().
 
Final Class
 
A class that may not be inherited from anything else.
 
Force unwrap
 
The process of using the value inside an optional without checking it exists first. If optional is empty - if it has no value - force unwrapping will crash your code.
 
Guard
 
A piece of Swift syntax that checks whether a condition is true, and force you to exit the current scope immediately if it is not. This is a commonly used guard which checks whether a has a value, and if it does, create a new constant for optional's value so it can be used safely. If it has no value, the guard condition fails and you must exist current scope.
 
Hashable
 
A Common Swift protocol that says conforming types can be represented using hash values.
 
Higher-Order function
 
A function that accepts another function as a parameter or sends back a function as its return value.
 
Initializer
 
A special method that gets run to create an instance of structs or class. You can have many initializers, and in the case of classes may call parent initializers inside your own initializer.
 
inout parameters
 
A function parameter that, when changed inside the function, remains changed outside the function.
 

Summary

 
I hope you understood, I will try to explain Swift common terms in Swift programming.
 
If you enjoyed reading my article, please share it and recommend it to others. Thank you in advance.
 
Happy programming!!! 


Similar Articles