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,
- if #available(iOS 13, *) {
- // use SwiftUI framework
- } else {
- // show alert
- }
@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
- var complitionHandler: ((Int)->Void)?
- func getSumOf(array:[Int], handler: @escaping ((Int)->Void)) {
- //here I'm taking for loop just for example, in real case it'll be something else like API call
- var sum: Int = 0
- for value in array {
- sum += value
- }
- self.complitionHandler = handler
- }
- func doSomething() {
- self.getSumOf(array: [16,756,442,6,23]) { [weak self](sum) in
- print(sum)
- //finishing the execution
- }
- }
- //Here we are storing the closure for future use.
- //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,
- protocol Container {
- associatedtype Item
- mutating func append(_ item: Item)
- var count: Int { get }
- subscript(i: Int) -> Item { get }
- }
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.

Dipa MehtaPosted Mar 11, 2020, 3:50 AM
Nice article Purvesh. Nice terms of Swift.
Dipa MehtaPosted Dec 5, 2019, 1:18 AM
An interesting topic to read on swift.