API
Essence
An API (Application Programming Interface) is a contract between two pieces of code: a set of types, functions and rules that one side publishes and the other uses without knowing the internal implementation. The implementation may change as long as the contract holds.
The APIs an iOS developer meets
- System frameworks: Foundation, UIKit, SwiftUI, Swift Concurrency — Apple publishes the interface, the implementation is closed.
- Third-party libraries (via Swift Package Manager): the package's public interface is everything marked
public/open. - Network APIs (REST, GraphQL, gRPC): the contract is the endpoints, the JSON format, the error codes. On the client it is mirrored by
Codablemodels. - Your own code: a module, a protocol, a class — anything other parts of the app use.
What a good API looks like in Swift
import Foundation
// The contract: what the service can do. How — is hidden.
public protocol ImageLoading: Sendable {
func data(for url: URL) async throws -> Data
}
public struct ImageLoader: ImageLoading {
public init() {}
public func data(for url: URL) async throws -> Data {
let (data, _) = try await URLSession.shared.data(from: url)
return data
}
private func cacheKey(for url: URL) -> String { // not part of the API
url.absoluteString
}
}
@available(iOS 26, *)
public func liquidGlassEffect() {} // availability is part of the contract too
- Access levels (
public,internal,private) draw the API boundary: the smallest necessary surface is the easiest to maintain. - A protocol instead of a concrete type lets you swap the implementation (a mock in tests, a cache in production).
@available,throws,async,Sendableare part of the contract: they tell the caller when and how a call is allowed.- Apple's API Design Guidelines: clarity at the call site, verb names for mutating methods (
sort()/sorted()).
API vs framework vs SDK
- API — the interface.
- Library/framework — ready-made code behind that interface (UIKit is a framework with its own API).
- SDK — a set of frameworks, tools and documentation (the iOS SDK shipped with Xcode).
What the interviewer looks for
Do you understand that changing a public API is a breaking change for every consumer, and that inside an app module boundaries should also be designed as APIs: through protocols and minimal visibility.