Advanced Formatting Guidelines
We will go through advanced formatting situations, architectural patterns, and complex Swift/Objective-C features for the SideStore project.
Advanced Swift Patterns
Protocol-Oriented Programming
Protocol Definitions
- Keep protocols focused and cohesive
- Use associated types for generic protocols
- Provide default implementations in extensions when appropriate
// ✅ Good
protocol AppInstalling {
associatedtype AppType: App
func install(_ app: AppType) async throws
func uninstall(_ app: AppType) async throws
}
extension AppInstalling {
func validateApp(_ app: AppType) -> Bool {
return !app.identifier.isEmpty && app.version.isValid
}
}
// ❌ Bad
protocol AppManager {
func install(_ app: Any) -> Bool
func uninstall(_ app: Any) -> Bool
func update(_ app: Any) -> Bool
func backup(_ app: Any) -> Bool
func restore(_ app: Any) -> Bool
// Too many responsibilities
}