Swift Literals and how it can be leveraged in custom types?
Initialize custom types with literals
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.
let x = 56
Literal types:
- IntegarLiteralType:
Integer literal type can be represented in following ways:
- Decimal Literal (Base 10): In this representation the allowed digits are 0–9.
// The octal number should be prefix with 0.
let x = 0146;
2. Octal Literal (Base 8): In this representation the allowed digits are n this form the allowed digits are 0–7.
// The octal number should be prefix with 0.
let x = 0148
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.
// The Hexadecimal number should be prefix with 0x
let x = 0x1bcd
4. Binary Literal: Literal values can by specified in binary form too. Allowed values are 0 & 1 and should be prefixed with 0b.
// Should be prefix with 0b
let x = 0b1111
- BooleanLiteralType:
Only two values are allowed for Boolean literals i.e. true and false.
let isVisible = true
- FloatLiteralType:
For Floating-point data types, we can specify literals in only decimal form.
let x = 5.18
- StringLiteralType:
let name = "Apple"
Initialization with Literals:
Swift provides us some literal protocols through which custom types can conform.
ExpressibleByBooleanLiteral:
A type that can be initialized with the Boolean literals true
and false
should conform to this protocol.
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.
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.
let threshold = 6.0
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 ExpressibleByStringLiteral
protocol. 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.