Skip to content

Instantly share code, notes, and snippets.

@Hexfire
Forked from brennanMKE/HTTPStatusCodes.swift
Created December 5, 2017 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hexfire/8e9a8cb0ee2e63861b8a0e931ca62eab to your computer and use it in GitHub Desktop.
Save Hexfire/8e9a8cb0ee2e63861b8a0e931ca62eab to your computer and use it in GitHub Desktop.
Swift Enums for HTTP Status Codes

Non-contiguous Enum Values

This is a feature of Swift Enumerations I did not know about. Clever.

Often values like status codes are tiered like HTTP status codes which are grouped together within a range and incremented by one for most values in that group. Reducing code which must be typed is a good practice with Swift and eliminating the need to set every value in an enum definition would be a great way to eliminate details which can be derived by convention.

For this example the HTTP Status Codes are used. Tiers are grouped at increments of 100 and most values within one of the previous before the next level. If the value is not specified it would simply be one more than the previous value.

A common practice with enum types is to start with defining the first value with 0 and incrementing each value by one for the rest of the values.

enum HTTPStatusCodes: Int {
// 100 Informational
case Continue = 100
case SwitchingProtocols
case Processing
// 200 Success
case OK = 200
case Created
case Accepted
case NonAuthoritativeInformation
case NoContent
case ResetContent
case PartialContent
case MultiStatus
case AlreadyReported
case IMUsed = 226
// 300 Redirection
case MultipleChoices = 300
case MovedPermanently
case Found
case SeeOther
case NotModified
case UseProxy
case SwitchProxy
case TemporaryRedirect
case PermanentRedirect
// 400 Client Error
case BadRequest = 400
case Unauthorized
case PaymentRequired
case Forbidden
case NotFound
case MethodNotAllowed
case NotAcceptable
case ProxyAuthenticationRequired
case RequestTimeout
case Conflict
case Gone
case LengthRequired
case PreconditionFailed
case PayloadTooLarge
case URITooLong
case UnsupportedMediaType
case RangeNotSatisfiable
case ExpectationFailed
case ImATeapot
case MisdirectedRequest = 421
case UnprocessableEntity
case Locked
case FailedDependency
case UpgradeRequired = 426
case PreconditionRequired = 428
case TooManyRequests
case RequestHeaderFieldsTooLarge = 431
case UnavailableForLegalReasons = 451
// 500 Server Error
case InternalServerError = 500
case NotImplemented
case BadGateway
case ServiceUnavailable
case GatewayTimeout
case HTTPVersionNotSupported
case VariantAlsoNegotiates
case InsufficientStorage
case LoopDetected
case NotExtended = 510
case NetworkAuthenticationRequired
}
// Objective-C works too
typedef NS_ENUM(NSUInteger, SituationStatus) {
SituationStatusNormal = 100,
SituationStatusAlright,
SituationStatusDecent,
SituationStatusGood = 200,
SituationStatusGreat,
SituationStatusAwesome,
SituationStatusNoGood = 300,
SituationStatusBad,
SituationStatusTerrible
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment