- Swift - Access Control
- Swift - Generics
- Swift - Protocols
- Swift - Extensions
- Swift - Type Casting
- Swift - Optional Chaining
- Swift - ARC Overview
- Swift - Deinitialization
- Swift - Initialization
- Swift - Inheritance
- Swift - Subscripts
- Swift - Methods
- Swift - Properties
- Swift - Classes
- Swift - Structures
- Swift - Enumerations
- Swift - Closures
- Swift - Functions
- Swift - Dictionaries
- Swift - Sets
- Swift - Arrays
- Swift - Characters
- Swift - Strings
- Swift - Loops
- Swift - Decision Making
- Swift - Operators
- Swift - Literals
- Swift - Constants
- Swift - Tuples
- Swift - Optionals
- Swift - Variables
- Swift - Data Types
- Swift - Basic Syntax
- Swift - Environment
- Swift - Overview
- Swift - Home
Swift Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Swift - Literals
A pteral is the source code representation of a value of an integer, floating-point number, or string type. The following are examples of pterals −
42 // Integer pteral 3.14159 // Floating-point pteral "Hello, world!" // String pteral
Integer Literals
An integer pteral can be a decimal, binary, octal, or hexadecimal constant. Binary pterals begin with 0b, octal pterals begin with 0o, and hexadecimal pterals begin with 0x and nothing for decimal.
Here are some examples of integer pterals −
let decimalInteger = 17 // 17 in decimal notation let binaryInteger = 0b10001 // 17 in binary notation let octalInteger = 0o21 // 17 in octal notation let hexadecimalInteger = 0x11 // 17 in hexadecimal notation
Floating-point Literals
A floating-point pteral has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point pterals either in decimal form or hexadecimal form.
Decimal floating-point pterals consist of a sequence of decimal digits followed by either a decimal fraction, a decimal exponent, or both.
Hexadecimal floating-point pterals consist of a 0x prefix, followed by an optional hexadecimal fraction, followed by a hexadecimal exponent.
Here are some examples of floating-point pterals −
let decimalDouble = 12.1875 let exponentDouble = 1.21875e1 let hexadecimalDouble = 0xC.3p0
String Literals
A string pteral is a sequence of characters surrounded by double quotes, with the following form −
"characters"
String pterals cannot contain an unescaped double quote ("), an unescaped backslash (), a carriage return, or a pne feed. Special characters can be included in string pterals using the following escape sequences −
Escape sequence | Meaning |
---|---|