Advanced and Practical Enum usage in Swift

API Endpoints

API Endpoints

Rest APIs are a great use case for enums. They're naturally grouped, they're limited to a finite set of APIs, and they may have additional query or named parameters which can be modelled through associated values.

Take, for example, a look at a simplified version of the Instagram API

enum Instagram {
  enum Media {
    case popular
    case shortcode(id: String)
    case search(lat: Float, 
                min_timestamp: Int, 
                lng: Float, 
                max_timestamp: Int, 
                distance: Int)
  enum Users {
    case user(id: String)
    case feed
    case recent(id: String)

Ash Furrow's Moya library is based around this idea of using enums to map rest endpoints.