The Basics Of Swift Programming Language
Welcome to the exciting world of Swift programming!
Whether you’re a seasoned developer looking to add a new language to your toolkit or a complete beginner taking your first steps into coding, Swift offers a powerful yet intuitive way to bring your ideas to life. In this blog post, “The Basics of Swift Programming Language” we’ll explore fundamental concepts like variables, constants, basic data types, typealias, and tuples. Let’s dive in and start coding!
Swift is a new programming language for iOS, macOS, watchOS, and tvOS app development. Many parts of the swift will be familiar with C and Objective C.
Swift provides its version of all fundamental C and Objective C types, including Int, Double, Float, Boolean, and String. Swift also offers a collection of types like Array, Set, and Dictionary. I will explain this collection type in the next blog.
Swift introduces advanced types not found in Objective C, such as TypeAlias, Tuples, etc.
Variables
Variables are used to store data. Variables means whose value can vary (i.e. change) during the execution of a program.
Variable start with var
keyword.
var name = "Swift"
var id: Int = 10 // Good Practice
var x = 0.0, y = 0.0, z = 0.0
var red, blue, green : String
Constants
Similar to variables, constants are also used to store data. But in the case of a constant, the value can not vary (i.e. change) during the execution of a program.
You can declare constants with the let
keyword.
let number = 10
let name: String = "Swift"
let marks: Double = 00
Comments
Comments are used by the programmers. Comments are not executed, so comments are called non-executable statements.
There are the following types of comments:
1. Single-line Comment
// This is a comment.
2. Multi-line Comments
/*
This is also comment
but it is written over multiple lines
*/
3. Nested Comments
/* This is the start of the first multiline comment.
/*
This is the second, nested multiline comment.
/*
This is the end of the first multiline comment.
/*
Semicolons
Semicolons are not required in Swift but if you want to write multiple separate statements on a single line then you need to put a semicolon after each statement.
let a = 10; print(a)
// Prints 10
Data Types
1. Integer
Used to store whole numbers without decimal values
let number: Int = 22
2. Float
Used to store decimal numbers with single precision. Flot represents a 32-bit floating-point number
let marks: Float = 22.11
3. Double
Used to store decimal numbers with double precision. Double represents a 64-bit floating-point number.
let marks: Float = 22.110011
4. Character
Used to store only a single character.
let a: Character = "A"
4. String
Used to store only a sequence of characters (textual data).
var name: String = "Swift Programming Language"
4. Boolean
Used to store logical entities (true or false)
let isEmpty: Bool = true
var passCheck = true
var failCheck = false
Type Alias
Type aliases define an alternative name for an existing type. You define type aliases with typealias
keyword.
typealias StudentName = String
let name: StudentName = "Manish Ahire"
Tuples
Tuples group multiple values into a single value. The values within tuples can be of any type. In the coming blogs, will explain tuples in more detail.
var product = ("MacBook Pro", 2024)
let httpError = (404, "Not Found")
Stay Connected🔔
Thank you for reading until the end. Before you go:
- 👏 Please consider clapping and following me!
- 🔗 Follow on LinkedIn | GitHub
- 📝 More content at mobodevstuff.com
- 🎨 Check out my portfolio at manishahire.com