1.1 KiB
1.1 KiB
Swift Best Practices
Code Style
- Follow Swift API Design Guidelines
- Use
camelCasefor variables/functions,PascalCasefor types - Prefer
letovervarwhen possible - Use optionals properly; avoid force unwrapping
// GOOD: Safe optional handling
func findUser(id: String) -> User? {
guard let user = repository.find(id) else {
return nil
}
return user
}
// Using optional binding
if let user = findUser(id: "123") {
print(user.name)
}
// BAD: Force unwrapping
let user = findUser(id: "123")! // Crash if nil
Error Handling
- Use
throwsfor recoverable errors - Use
Result<T, Error>for async operations - Handle all error cases explicitly
// GOOD: Proper error handling
func loadConfig() throws -> Config {
let data = try Data(contentsOf: configURL)
return try JSONDecoder().decode(Config.self, from: data)
}
do {
let config = try loadConfig()
} catch {
print("Failed to load config: \(error)")
}
Security
- Use Keychain for sensitive data
- Validate all user input
- Use App Transport Security (HTTPS)
- Never hardcode secrets