一键导入
swift
Google's official Swift style guide. Covers naming conventions, optionals, protocols, error handling, access control, formatting, and Swift best practices.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Google's official Swift style guide. Covers naming conventions, optionals, protocols, error handling, access control, formatting, and Swift best practices.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | swift |
| description | Google's official Swift style guide. Covers naming conventions, optionals, protocols, error handling, access control, formatting, and Swift best practices. |
Official Google Swift coding standards for consistent, maintainable iOS/macOS code.
let over var — immutability by default!)guard for early returns — reduce nesting with early exits| Element | Convention | Example |
|---|---|---|
| Types (class/struct/enum/protocol) | UpperCamelCase | UserService |
| Functions/Methods | lowerCamelCase | getUserById |
| Variables/Properties | lowerCamelCase | userCount |
| Constants | lowerCamelCase | maxRetries |
| Enums | UpperCamelCase | Direction |
| Enum cases | lowerCamelCase | .up, .down, .left |
| Type aliases | UpperCamelCase | CompletionHandler |
// ✓ CORRECT - prefer let (immutable)
let userName = "Alice"
let maxRetries = 3
// ✓ CORRECT - var only when value changes
var retryCount = 0
retryCount += 1
// ✓ CORRECT - type inference (no need to repeat type)
let users: [User] = [] // OK: collection type is clear
let count = users.count // inferred as Int
// ✓ CORRECT - if let for optional binding
if let user = getUser(id: 42) {
print(user.name)
}
// ✓ CORRECT - guard for early return
func processUser(id: Int) {
guard let user = getUser(id: id) else {
print("User not found")
return
}
// user is non-optional here
display(user)
}
// ✓ CORRECT - nil coalescing
let name = user?.name ?? "Unknown"
// ✗ INCORRECT - force unwrap
let user = getUser(id: 42)! // crashes if nil
// ✓ CORRECT - prefer struct for value types
struct User {
let id: Int
var name: String
var email: String
}
// ✓ CORRECT - class for reference semantics / inheritance
final class UserService {
private let repository: UserRepository
init(repository: UserRepository) {
self.repository = repository
}
func getUser(id: Int) async throws -> User {
return try await repository.find(id: id)
}
}
// ✓ CORRECT - protocol for abstraction
protocol UserRepository {
func find(id: Int) async throws -> User
func save(_ user: User) async throws
}
// ✓ CORRECT - protocol extension for default behavior
extension UserRepository {
func findAll() async throws -> [User] {
return []
}
}
// ✓ CORRECT - composing protocols
typealias UserStore = UserRepository & Codable
// ✓ CORRECT - typed errors with enum
enum UserError: Error {
case notFound(id: Int)
case invalidData(String)
case networkFailure(underlying: Error)
}
// ✓ CORRECT - throw and catch
func fetchUser(id: Int) throws -> User {
guard let user = database[id] else {
throw UserError.notFound(id: id)
}
return user
}
// ✓ CORRECT - do-catch
do {
let user = try fetchUser(id: 42)
display(user)
} catch UserError.notFound(let id) {
print("User \(id) not found")
} catch {
print("Unexpected error: \(error)")
}
// ✓ CORRECT - async/await (Swift 5.5+)
func loadUsers() async throws -> [User] {
let data = try await networkClient.get("/api/users")
return try JSONDecoder().decode([User].self, from: data)
}
// ✓ CORRECT - task for concurrent work
Task {
do {
let users = try await loadUsers()
await MainActor.run { self.users = users }
} catch {
await MainActor.run { self.error = error }
}
}
// ✓ CORRECT - explicit access control
public struct User {
public let id: Int
public var name: String
internal var metadata: [String: String]
private var _internalState: Bool
}
// ✓ CORRECT - use final to prevent subclassing
public final class AuthService {
// ...
}
// ✓ CORRECT - trailing closures
users.forEach { user in
print(user.name)
}
let activeUsers = users.filter { $0.isActive }
let names = users.map { $0.name }
// ✓ CORRECT - 2-space indentation, opening brace on same line
func process() {
if condition {
doSomething()
}
}
| Mistake | Correct Approach |
|---|---|
Force unwrapping (!) | Use if let, guard let, or nil coalescing |
var everywhere | Use let by default; var only when mutating |
| Class for everything | Prefer struct for value semantics |
| No error handling | Use throws / do-catch |
| Inheritance over protocols | Favor protocol composition |
| No access control | Explicitly mark public, internal, private |
| Implicit self in closures | Use [weak self] to avoid retain cycles |
npx skills add testdino-hq/google-styleguides-skills/swift
See swift.md for complete details, examples, and edge cases.
Complete collection of Google's official style guides for 17 languages. Includes TypeScript, JavaScript, Python, Java, Go, C++, C#, Swift, Objective-C, HTML/CSS, AngularJS, Shell, R, Common Lisp, Vim Script, JSON, and Markdown. Production-ready coding standards used across Google's engineering organization, formatted for AI agent consumption.
Google's official AngularJS style guide. Covers controllers, services, directives, modules, dependency injection, and AngularJS 1.x best practices.
Google's official Common Lisp style guide. Covers naming conventions, formatting, macros, packages, documentation strings, and Lisp best practices.
Google's official C++ style guide. Covers headers, naming conventions, formatting, classes, memory management, RAII, smart pointers, and modern C++ features.
Google's official C# style guide. Covers naming conventions, formatting, LINQ, async/await, XML documentation, and .NET best practices.
Google's official Go style guide. Covers gofmt formatting, naming conventions, error handling, interfaces, concurrency patterns, and package organization. Enforces idiomatic Go code with short variable names and explicit error checks.