Sitemap
iOS Dev

iOS Dev

I'm enthusiastic for iOS application development

Swift Feature — Part 2

3 min readJul 23, 2025

--

Closures, Generics, Property Wrapper, Macro

Closures

Closures are self-contained blocks of functionality that can be passed around and used in your code.

Closures in Swift are similar to closures, anonymous functions, lambdas, and blocks in other programming languages.

Closures are reference types,

  • Closures can capture and store references to any constants and variables from the context in which they’re defined.
  • This is known as closing over those constants and variables. Swift handles all of the memory management of capturing for you.

Non Escaping Closure / Closure expressions are a way to write inline closures in a brief, focused syntax.

{ (<#PRAMETERS#>) -> RETUEN TYPE in

<#STATEMENTS#>

}

Trailing Closure : passed at final argument of function as closure

Escaping Closure:

  • In Swift, an “escaping closure” is a closure that’s called after the function it was passed to has returned, meaning it outlives the function’s scope. To indicate a closure is escaping, you use the @escaping keyword.
  • When a closure is passed to a function, but is not called immediately within that function, it’s considered escaping.

Autoclosure : https://www.avanderlee.com/swift/autoclosure/

Protocols

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.

The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.

Any type that satisfies the requirements of a protocol is said to conform to that protocol.

Generics

Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define.

Get iOS Dev’s stories in your inbox

Join Medium for free to get updates from this writer.

You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.

https://developer.apple.com/videos/play/wwdc2022/110352/

Associated Types

  • When defining a protocol, it’s sometimes useful to declare one or more associated types as part of the protocol’s definition.
  • An associated type gives a placeholder name to a type that’s used as part of the protocol.
  • The actual type to use for that associated type isn’t specified until the protocol is adopted. Associated types are specified with the associatedtype keyword.

Opaque and Boxed Protocol Types

Swift provides two ways to hide details about a value’s type: opaque types and boxed protocol types.

  • Hiding type information is useful at boundaries between a module and code that calls into the module, because the underlying type of the return value can remain private.
  • A function or method that returns an opaque type hides its return value’s type information.

Property Wrappers

Properties associate values with a particular class, structure, or enumeration.

  • Stored properties store constant and variable values as part of an instance, whereas computed properties calculate (rather than store) a value. Stored properties are provided only by classes and structures.
  • Computed properties are provided by classes, structures, and enumerations.

A property wrapper adds a layer of separation between code that manages how a property is stored and the code that defines a property.

  • Like the name implies, a property wrapper is essentially a type that wraps a given value in order to attach additional logic to it — and can be implemented using either a struct or a class by annotating it with the @propertyWrapper attribute.
  • Besides that, the only real requirement is that each property wrapper type should contain a stored property called wrappedValue, which tells Swift which underlying value that’s being wrapped.
  • For example, let’s say that we wanted to create a property wrapper that automatically capitalizes all String values that were assigned to it. That might be implemented like this:
Press enter or click to view image in full size

Swift property wrappers offer advantages like

  • code reusability,
  • cleaner syntax,
  • and improved readability by encapsulating property-related logic, allowing you to apply custom behaviors to properties with minimal boilerplate.

Macros

Swift macros offer advantages like code generation at compile time, reducing boilerplate and increasing code expressiveness and readability, while also enabling developers to customize the Swift language for specific needs.

Swift has two kinds of macros:

  • Freestanding macros appear on their own, without being attached to a declaration.
  • Attached macros modify the declaration that they’re attached to.

You call attached and freestanding macros slightly differently, but they both follow the same model for macro expansion, and you implement them both using the same approach.

https://www.avanderlee.com/swift/macros/

--

--

iOS Dev
iOS Dev

Written by iOS Dev

0 followers

I'm enthusiastic for iOS application development