Saltar al contenido
Snippets·Gratis

Snippets de Swift

20 snippets de Swift: opcionales, enums y Result, protocolos con extensiones, async/await y URLSession, Codable, vistas y estado de SwiftUI, un modelo @Observable y XCTest. Algunos necesitan Swift 5.9 o iOS 17.

Se instala gratis.

Si tienes PocketCode instalado en tu Android, esto abre la página en la app.

Vista previa

5 de 20

Unwrap with if letiflet

Run a block only when the optional has a value

if let ${1:user} = ${2:currentUser} {
    $0
}

Switch over an enumswitchenum

Switch with one case per enum value

switch ${1:direction} {
case .${2:north}:
    $0
case .${3:south}:
    break
}

Protocol with a default implementationprotoc

Protocol plus an extension that every conforming type gets for free

protocol ${1:Describable} {
    var ${2:summary}: String { get }
}

extension ${1:Describable} {
    func describe() {
        print(${2:summary})
    }
}

Async throwing functionasyncf

Function called with try await

func ${1:fetchUser}(id: ${2:Int}) async throws -> ${3:User} {
    $0
}

Task with error handlingtaskb

Start async work from synchronous code and catch its errors

Task {
    do {
        ${1:try await load()}
    } catch {
        print(error)
    }
}

Cambios en la 1.0.0

Primera versión.

Relacionados