Swift Literals and how it can be leveraged in custom types?

Initialize custom types with literals

Aaina jain
Swift India
3 min readAug 21, 2018

--

Credit: Unsplash

Literals?

Literals are a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variables.

In swift standard library typealias of primitive types are created. Whenever we assign any value to variable with no type information, Swift uses these type aliases to determine what type to use for the expression.

Literal types:

  • IntegarLiteralType:

Integer literal type can be represented in following ways:

  1. Decimal Literal (Base 10): In this representation the allowed digits are 0–9.

2. Octal Literal (Base 8): In this representation the allowed digits are n this form the allowed digits are 0–7.

3. Hexa-decimal Literal (Base 16): In this representation the allowed digits are 0–9 and characters are a-f. Both uppercase and lowercase characters are permitted.

4. Binary Literal: Literal values can by specified in binary form too. Allowed values are 0 & 1 and should be prefixed with 0b.

  • BooleanLiteralType:

Only two values are allowed for Boolean literals i.e. true and false.

  • FloatLiteralType:

For Floating-point data types, we can specify literals in only decimal form.

  • StringLiteralType:

Initialization with Literals:

Swift provides us some literal protocols through which custom types can conform.

Literal protocols

ExpressibleByBooleanLiteral:

A type that can be initialized with the Boolean literals true and false should conform to this protocol.

ExpressibleByBooleanLiteral

ExpressibleByIntegerLiteral:

The standard library integer and floating-point types, such as Int and Double, conform to the ExpressibleByIntegerLiteral protocol. You can initialize a variable or constant of any custom type who conform to ExpressibleByIntegerLiteral by assigning an integer literal.

ExpressibleByIntegerLiteral

ExpressibleByFloatLiteral:

The standard library floating-point types — Float, Double all conform to the ExpressibleByFloatLiteral protocol. You can initialize a variable or constant of any of these types by assigning a floating-point literal.

ExpressibleByNilLiteral:

Only the Optional type conforms to ExpressibleByNilLiteral. It’s public protocol but Apple discourage to conform to this protocol for custom types.

ExpressibleByStringLiteral:

The String and StaticString types conform to the ExpressibleByStringLiteralprotocol. You can initialize a variable or constant of either of these types using a string literal of any length.

ExpressibleByStringLiteral stands for a type that can be initialized with a string literal. The initializer init(stringLiteral: Self.StringLiteralType) is the only method that needs to be implemented for this protocol conformance.

ExpressibleByExtendedGraphemeClusterLiteral stands for the type that can be initialized with a string containing a single extended grapheme cluster, e.g. “ந”. More about the extended grapheme clusters can be found in Unicode standard.

ExpressibleByUnicodeScalarLiteral can be initialized with a string containing a single Unicode Scalar value. e.g. “♥”.

If you regularly hard-code URLs and are tired of force unwrapping them when you know they are correct, you can make URL conform to ExpressibleByStringLiteral so that URLs can be created directly from strings:

This protocol can also be use to create date from string with formatter.

ExpressibleByArrayLiteral:

Conform to ExpressibleByArrayLiteral protocol when implementing custom collections that are backed by an Array. More details

ExpressibleByDictionaryLiteral:

ExpressibleByDictionaryLiteral is primarily used for custom collections and types that are backed by a dictionary. More details

Conclusion:

Literal protocols are good to use when you know there will be value always. You can’t return nil from literal initializer.

You can catch me at:

Linkedin: Aaina Jain

Twitter: __aainajain

If you have any suggestions for the next post write to me at aainajain100@gmail.com.

--

--