Pular para o conteúdo
Snippets·Grátis

Snippets de Swift

20 snippets de Swift: opcionais, enums e Result, protocolos com extensões, async/await e URLSession, Codable, views e estado do SwiftUI, um modelo @Observable e XCTest. Alguns precisam do Swift 5.9 ou iOS 17.

Instalação grátis.

Se o PocketCode estiver instalado no seu Android, isto abre a página no app.

Prévia

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)
    }
}

Mudanças na 1.0.0

Primeira versão.

Relacionados