Part 1 — The Basics of Swift
In this article will cover the very basics of Swift
Documentation Series of Swift
Part 1 — The Basics of Swift
In this article will cover the very basics of Swift
Constants and Variables
To define variable we can use let and var
let — it’s value can never be changed
let maximumNumber = 10
let languageName = "Swift"
languageName = "Swift++"
// This is a compile-time error: languageName cannot be changed.
var — it’s value can be changed
var friendlyWelcome = "Hello!"
friendlyWelcome = "Bonjour!"
// friendlyWelcome is now "Bonjour!"
Types of annotations
It is providing types to the variable or constant while defining it.
var welcomeMessage: String
Defining multiple related variables
var red, green, blue: Double
Naming constants and variables
Constant and variable names can’t contain whitespace characters, mathematical symbols, arrows, private-use Unicode scalar values, or line- and box-drawing characters. Nor can they begin with a number, although numbers may be included elsewhere within the name.
var Welcome = "Hello"
print("\(Welcome) Nafish")
Comments
To write Single line comment use // …..
//.........
To write multiple line comment use / ..… /
/* .............
..............*/
Semicolons
We don’t need to use any Semicolon after each statement but you can use it to write multiple statements in single line like
let cat = "Luna"; print(cat)
Integers
Integers are whole numbers with no fractional component, such as 42 and -23. Integers are either signed (positive, zero, or negative) or unsigned (positive or zero).
let Number: Int = 3
Floating-Point Numbers
Floating point numbers have a fractional component, such as 3.14159, 0.1, and -273.15.
let Number: Float = 3.123
Integer and floating-point conversion
You can convert int to float by following ways
let three = 3
let pointOneFour = 0.14
let pi = Double(three) + pointOnefour
Boolean
It is called as Bool, it has two values True and False mostly useful for conditional statements.
Tuples
It can contain multiple types of values in single tuple
let httpError = (404, "Not Found")
Where 404 in int and Not Found is String.
You can name the individual elements in tuple like
let http200Status = (statusCode: 200, description: "OK")
print(http200Status.statusCode)
Optionals
It represents a variable that can either have a value or can be nil
let Number : Int? = 124
Force Unwrapping (!) we can use ! to unwrap to the end of optional name,when you force unwrap a non-nil value, the result is its unwrapped value.
Error Handling
We use error handling to respond to error conditions.
func canThroughError() throws{
}
Assertions and Preconditions
We use these to make sure essential conditions is satisfied before executing code.
메타데이터
- post_id
- 2eeaaf5f9a1e
- slug
- the-basics-of-swift-2eeaaf5f9a1e
- url
- https://medium.com/@nafishuddin2022/the-basics-of-swift-2eeaaf5f9a1e
- canonical_url
- https://medium.com/@nafishuddin2022/the-basics-of-swift-2eeaaf5f9a1e
- author_url
- https://medium.com/@nafishuddin2022
- status
- ok
- fetched_at
- 2026-06-09 15:37:30