ワンクリックで
swift-coding-standards
Master Swift coding standards with Apple's guidelines, protocol-oriented design, and modern concurrency patterns
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Master Swift coding standards with Apple's guidelines, protocol-oriented design, and modern concurrency patterns
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
| name | swift-coding-standards |
| category | coding-standards |
| difficulty | intermediate |
| estimated_time | 45 minutes |
| description | Master Swift coding standards with Apple's guidelines, protocol-oriented design, and modern concurrency patterns |
| version | 1.0.0 |
| tags | ["swift","ios","macos","mobile","apple"] |
| related_skills | ["kotlin-coding-standards","typescript-coding-standards"] |
Learn Once, Write Anywhere: Master Swift coding standards for iOS, macOS, watchOS, and tvOS development with protocol-oriented design, value semantics, and modern concurrency.
// Optional binding (safe unwrapping)
if let name = name { print("Hello, \(name)") }
// Guard statement (early exit)
guard let age = age else { return }
// Nil coalescing (default value)
let displayName = name ?? "Anonymous"
// Optional chaining
let count = user?.profile?.posts?.count
// Protocol definition with default implementation
protocol Drawable {
var color: Color { get set }
func draw()
}
extension Drawable {
func draw() { print("Drawing with \(color)") }
}
// Struct with protocol conformance
struct Circle: Drawable {
var color: Color
var radius: Double
}
// Closure syntax
let doubled = numbers.map { $0 * 2 }
// Property wrapper
@State private var isPresented = false
// Types: PascalCase
class UserProfileViewController { }
struct NetworkRequest { }
protocol DataSource { }
// Variables/Functions: camelCase
var userName: String
func fetchUserProfile() { }
// Booleans: use is/has/should prefix
var isLoading: Bool
var hasCompletedOnboarding: Bool
Swift's optional system eliminates null pointer exceptions through compile-time safety.
// Safe unwrapping patterns
if let unwrapped = optionalValue {
print(unwrapped)
}
// Multiple bindings
if let name = userName, let age = userAge, age >= 18 {
print("\(name) is an adult")
}
// Guard for early exit
func processUser(_ user: User?) {
guard let user = user else { return }
print(user.name)
}
// Optional map/flatMap
let uppercasedName = userName.map { $0.uppercased() }
let userID = userIDString.flatMap { Int($0) }
Swift favors composition over inheritance through protocols.
// Define capabilities through protocols
protocol DataStore {
func save<T: Codable>(_ item: T, key: String) throws
func load<T: Codable>(key: String) throws -> T?
}
// Protocol extension for default behavior
extension Collection {
func chunked(into size: Int) -> [[Element]] {
stride(from: 0, to: count, by: size).map {
Array(self[$0..<Swift.min($0 + size, count)])
}
}
}
// Associated types for flexibility
protocol Repository {
associatedtype Item
func fetchAll() async throws -> [Item]
func save(_ item: Item) async throws
}
// Use struct (value type) for:
// - Simple data models, independent copies
struct User {
var name: String
var email: String
}
// Use class (reference type) for:
// - Shared mutable state, object identity matters
class NetworkManager {
static let shared = NetworkManager()
}
// Use enum for:
// - Finite set of options, state machines
enum LoadingState {
case idle, loading
case success(Data)
case failure(Error)
}
// Breaking cycles with weak
class Apartment {
weak var tenant: Person? // weak breaks the cycle
}
// Closure capture lists
onComplete = { [weak self] in
self?.processResult() // Safe optional call
}
// Use unowned when reference should never be nil
class CreditCard {
unowned let customer: Customer // Card can't exist without customer
}
enum NetworkError: Error {
case invalidURL
case noConnection
case serverError(statusCode: Int)
}
func fetchUser(id: Int) throws -> User {
guard let url = URL(string: "https://api.example.com/users/\(id)") else {
throw NetworkError.invalidURL
}
// ... implementation
}
// Calling throwing functions
do {
let user = try fetchUser(id: 123)
} catch NetworkError.serverError(let code) {
print("Server error: \(code)")
} catch {
print("Unknown error: \(error)")
}
// Async functions
func fetchUser(id: Int) async throws -> User {
let url = URL(string: "https://api.example.com/users/\(id)")!
let (data, _) = try await URLSession.shared.data(from: url)
return try JSONDecoder().decode(User.self, from: data)
}
// Parallel execution with TaskGroup
func fetchAllUsers(ids: [Int]) async throws -> [User] {
try await withThrowingTaskGroup(of: User.self) { group in
for id in ids {
group.addTask { try await fetchUser(id: id) }
}
return try await group.reduce(into: []) { $0.append($1) }
}
}
// Actors for thread-safe state
actor BankAccount {
private var balance: Double = 0
func deposit(_ amount: Double) { balance += amount }
func getBalance() -> Double { balance }
}
// MainActor for UI updates
@MainActor
class ViewModel: ObservableObject {
@Published var isLoading = false
@Published var users: [User] = []
}
import XCTest
@testable import MyApp
final class UserManagerTests: XCTestCase {
var sut: UserManager!
var mockStore: MockDataStore!
override func setUp() {
super.setUp()
mockStore = MockDataStore()
sut = UserManager(store: mockStore)
}
func testFetchUser_Success() async throws {
// Arrange
mockStore.userToReturn = User(id: 1, name: "John")
// Act
let user = try await sut.fetchUser(id: 1)
// Assert
XCTAssertEqual(user.name, "John")
XCTAssertTrue(mockStore.fetchUserCalled)
}
}
For comprehensive examples and advanced patterns, see REFERENCE.md.
# Install SwiftLint
brew install swiftlint
# Add to Xcode build phase
if which swiftlint >/dev/null; then swiftlint; fi
# Run manually
swiftlint lint --config .swiftlint.yml
config/.swiftlint.yml - SwiftLint configurationtemplates/ViewModel.swift - MVVM templatetemplates/NetworkService.swift - Async network layertemplates/TestCase.swift - XCTest templateRemember: Swift is designed for safety, speed, and expressiveness. Embrace optionals, leverage protocols, and write tests.
Orchestration & Events:
Kubernetes standards for container orchestration, deployments, services, ingress, ConfigMaps, Secrets, and security policies. Covers production-ready configurations, monitoring, and best practices for cloud-native applications.
Master Kotlin coding standards with null safety, coroutines, and idiomatic patterns. Use when developing JVM/Android applications requiring type-safe async programming.
Comprehensive coding standards and best practices for maintainable, consistent software development across multiple languages and paradigms
React frontend standards covering hooks (useState, useEffect, useContext, custom hooks), state management (Context API, Redux, Zustand), performance optimization (memoization, lazy loading, code splitting), testing with React Testing Library, and accessibility (WCAG 2.1, ARIA) for modern SPAs
Security Operations Center (SOC) practices, incident response, SIEM management, and threat hunting following NIST 800-61