All skills
dpearson2699 avatar

/swift-language

@cf3fe87

Apply modern Swift language patterns and idioms for non-concurrency, non-SwiftUI code. Covers if/switch expressions (Swift 5.9+), typed throws (Swift 6+), result builders, property wrappers, opaque and existential types (some vs any), guard patterns, Never type, Regex builders (Swift 5.7+), basic Codable shaping (CodingKeys, custom decoding, nested containers), modern collection APIs (count(where:), contains(where:), replacing()), basic FormatStyle usage, and string interpolation patterns. Use when writing core Swift code involving generics, protocols, enums, closures, or modern language features; route deep Codable to swift-codable, detailed formatting/localization to swift-formatstyle, and API naming to swift-api-design-guidelines.

Use this Skill: https://skilld.dev/gh/dpearson2699/swift-ios-skills/swift-language

This session only. Nothing lands on disk.

referencesswift-attributes-interop.md

≈956 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Swift Attributes and C Interoperability

Attributes and interoperability features for Swift. Covers C-calling-convention export, module disambiguation, performance annotations, and symbol visibility control.

Contents

C Interoperability — @c Attribute

For functions, the @c attribute (SE-0495) marks a Swift declaration for direct C-calling-convention export. The function becomes callable from C, C++, and Objective-C without bridging headers or @_cdecl.

@c(MyLib_processBuffer)
public func processBuffer(_ buffer: UnsafePointer<UInt8>?, _ count: Int32) -> Int32 {
    guard let buffer else { return 0 }
    return buffer.pointee == 0 ? 0 : count
}

Requirements:

  • Parameters and return types must be C-compatible (primitives, pointers, tuples of C-compatible types)
  • No Swift-only types (String, Array, UnsafeBufferPointer, closures, generic placeholders, etc.) in the signature
  • The function must be a module-level free function (not a method)
  • Use @c(CustomName) when the C symbol should differ from the Swift function name
  • SE-0495 also supports @c enums with C-compatible integer raw types; this section focuses on function export

Module Selectors

SE-0491 adds ModuleName::symbolName syntax to disambiguate identically named symbols from different modules without import aliasing.

import NetworkingA
import NetworkingB

// Both modules export a top-level `configure()` function
func setup() {
    NetworkingA::configure()
    NetworkingB::configure()
}

// Works with types too
let client: NetworkingA::Client = .init()

Performance Annotations

@specialized

SE-0460 makes @specialized an official attribute (previously underscored as @_specialize). Forces the compiler to emit a specialized version of a generic function for specific concrete types.

@specialized(where T == Int)
@specialized(where T == Double)
func sum<T: Numeric>(_ values: [T]) -> T {
    values.reduce(.zero, +)
}

@inline(always) Guarantee

SE-0496 makes @inline(always) a guaranteed inlining request for direct function references. Previously it was a hint the compiler could ignore. A compilation error is now emitted when the compiler can prove direct-call inlining is impossible (e.g., recursive calls).

@inline(always)
func fastPath(_ x: Int) -> Int {
    x &+ 1  // Guaranteed for direct calls that can be inlined
}

The guarantee does not apply to dynamic calls through first-class function values, protocol values, generic constraints, or non-final class dispatch.

Symbol Visibility and Layout

@export

SE-0497 gives explicit control over symbol visibility and definition availability:

  • @export(interface) — ensures a callable symbol exists in the binary but hides the definition from clients (no inlining/specialization by external callers). Replaces @_neverEmitIntoClient.
  • @export(implementation) — makes the definition available for inlining/specialization but does not guarantee a callable symbol. Replaces @_alwaysEmitIntoClient.
@export(interface)
public func stableAPI() -> Int {
    // Callable symbol guaranteed; definition hidden from clients
    return computeValue()
}

@section and @used

SE-0492 places global or static variables into named binary sections and prevents dead-stripping. Primarily for Embedded Swift and systems programming.

@section(".mydata") @used
var configFlag: Int32 = 1

Use it only on stored variables in non-generic contexts with compile-time-known or static initialization.

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The analyzed skill consists of Swift language reference documentation and configuration files for a code generation agent. The documents instruct the agent on proper application of idiomatic Swift 5.9 and 6+ language features while respecting architecture boundaries. No security vulnerabilities, malicious patterns, or obfuscation techniques were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    2 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at cf3fe87. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 3 days ago.

Steadyupdated 3 months ago
  • swift
  • ios
  • language-features
  • generics
  • protocols
  • codable
  • regex
  • property-wrappers
  • result-builders
  • typed-throws

README badge

README badge for dpearson2699/swift-ios-skills/swift-language

Teaches modern Swift language patterns including if/switch expressions, typed throws, result builders, property wrappers, opaque and existential types, guard patterns, Regex builders, and Codable best practices. Targets Swift 5.7+ and 6+ features for core iOS and Swift code that does not involve concurrency or SwiftUI.

Generated from the current SKILL.md.

Does this cover async/await and actors?
No. Concurrency patterns (actors, async/await, Sendable) are in the separate swift-concurrency skill. This skill focuses on non-concurrency language features.
What Swift versions does this target?
Swift 6.3 is the primary target. Features like if/switch expressions require Swift 5.9+, typed throws require Swift 6+, and regex builders require Swift 5.7+.
Does this include SwiftUI patterns?
No. SwiftUI views and state management are covered in the separate swiftui-patterns skill. This skill is for core Swift code.
Does this cover property wrappers for SwiftUI state?
This skill explains property wrappers as a language feature with general examples. SwiftUI-specific wrappers like @State and @Binding are in the swiftui-patterns skill.
Can I use opaque types (some) in function parameters?
Yes, since Swift 5.9+. Using `some Protocol` in parameter position is equivalent to a generic constraint and is preferred over writing explicit generics.

Generated from the current SKILL.md. These answers refresh after source changes.