Swift Programming Fundamentals
Hey there! So, guess what? I’m diving into this cool world of Swift programming for my MADD module (that’s Mobile Application Design and Development, by the way) in my 4th year of university. It’s like unlocking a new level in the tech universe! Right now, I’m getting the hang of the basics, like using Xcode and the Swift language that Apple cooked up. Imagine, I can create apps for iPhones and stuff! We started with the classic “Hello World” example, and it’s way simpler than I thought. I’m also playing around with numbers, strings (like those text messages we send), and these nifty things called Booleans for making decisions in my code.
About Swift
Understand the high-level goals of the language.
Swift is a cool and easy-to-learn computer language made by Apple. You can start making apps for iPhones and Mac computers with the stuff you'll learn from this. The main thing you need to start learning Swift is Xcode, which is a special program that helps you write code. Inside Xcode, there’s a tool called the Swift compiler, as well as some kits for making apps for iPhones and Macs. These kits have everything you need to support the apps you make.
The Technologies
The following technologies are all part of your journey into the Swift language.
Swift
Swift is like a new and updated language made to be both easy to learn and super strong. It’s the language Apple picked to help apps grow on iPhones and Macs. It’s the language that powers up the apps in the Apple world.
Xcode
Xcode is like Apple’s main place for making apps. It’s got a bunch of tools, like an editor for writing code, a debugger to find and fix problems, a project manager to organize everything and a compiler tool to change Swift code into code that works on iPhones or Macs. You can grab Xcode from Apple’s Mac App Store for free.
LLVM
Even though you might not see it, there’s this thing called LLVM in Xcode that makes Swift awesome. LLVM is like the magic tool that takes the coolness of Swift and turns it into the computer language that makes Apple devices work. It’s basically what makes Swift code turn into the bits and bytes that our Apple devices understand.
Hello World
Okay, so when you’re learning a new language, it’s like a tradition to start with a simple “Hello World” example. Swift makes it super easy. Here’s how you do it:
print(“Hello World”)
See, you just write print("Hello World")
, and Swift will show "Hello World" when you run your code. It's like the first thing you do to make sure everything's working. Simple, right?
Now, let’s make it a bit more personal by checking out a cool feature in Swift called “string interpolation.”
It’s like a trick where we can put values right into our strings using the (…) trick
. Look at this:
let name: String = "Madhusha"
print("Hello \(name)!") // Prints "Hello Madhusha!"
Here, we’ve stuck the value of the name
variable right into the middle of our string. So, if your name is, say, Prasad, it will print "Hello, Prasad!" when you run it. Swift lets us do these neat things to make our messages more personalized. Cool, huh?
When to use let vs var
Alright, let’s talk about when to use let
and when to use var
in Swift.
- let: Use
let
when you want to create a constant. A constant is like a box that holds a value, and once you put something in it, you can’t change it. It’s like saying, “This value is set, and it’s not gonna change.”
swift
let pi = 3.14
Here, pi
is a constant, and its value is always 3.14.
- var: Use
var
when you want to create a variable. A variable is also a box for a value, but withvar
, you can change what’s inside. It’s like saying, “I might wanna swap this value out for another one later.”
swift
var score = 100
In this example, the score
is a variable, and you can change its value whenever you want.
So, in short:
- Use
let
for things that won’t change. - Use
var
for things that might change.
Remember, it’s like deciding if you’re making a box that’s stuck with one thing forever (let
) or a box that can hold different things at different times (var
).
Numbers
We use numbers in programming for all sorts of things: counting, calculating, measuring, you name it. In Swift, there are a few types of numbers you need to know about:
1. Integers (Int
): These are whole numbers without any decimal places. For example:
let age : Int = 17
Here, age is an integer because you can’t be, say, 17.5 years old.
2. Doubles and Floats (Double
and Float
): These are numbers with decimal places. If you need more precision, you use Double
, and if you’re okay with a bit less precision but save some memory, you use Float
.
let pi = 3.14
let price = 19.99
let billAmount: Double = 10.25
Here, pi
and price
are numbers with decimals, so we use Double
by default.
3. Arithmetic Operations: You can do math stuff with numbers in Swift. Addition (+
), subtraction (-
), multiplication (*
), and division (/
) all work as you’d expect.
let sum = 5 + 3 // sum is now 8
let product = 4 * 2 // product is now 8
let difference = 10–5 // difference is now 5
let quotient = 20 / 4 // quotient is now 5
Remember, when you work with numbers in Swift, make sure you’re using the right type for what you’re doing. If you want decimals, use Double
or Float
. If you’re dealing with whole numbers, use Int
. Swift likes to keep things organized!
Strings
Alright, let’s dive into strings in Swift. In programming, a string is a bunch of characters put together. It can be your name, a sentence, or anything that’s made up of letters, numbers, or symbols.
1. Creating a String:
You can create a string by putting characters inside double quotes. Here’s an example:
let greeting : String = “Hello, World!”
Here, greeting
is a string that says “Hello, World!”
2. String Interpolation:
Remember when we talked about making things personal? Swift has a cool trick called string interpolation. You can put variables or expressions right into a string using \(…):
let name : String = “John”
let personalizedGreeting : String = “Hello, \(name)!”
Now, personalizedGreeting
is “Hello, John!”.
3. String Operations:
You can also combine strings using the +
operator:
let part1 : String = “Hello, “
let part2 : String = “World!”
let combined : String = part1 + part2
Now, combined is “Hello, World!”.
4. Multiline Strings:
If you have a long piece of text, you can use triple quotes ”””
to create a multiline string:
let multilineText = """
This is a
multiline
string.
"""
This is handy when you want to keep your text nice and formatted.
Remember, strings are your way of dealing with text in Swift. Whether it’s a simple greeting or a whole paragraph, Swift’s got you covered!
Booleans
Alright, let’s talk about Booleans in Swift. Booleans are like little switches that can be either on or off, true or false.
1. Creating Booleans:
You can create a Boolean variable like this:
let isRaining = true
let isSunny = false
Here, isRaining
is set to true
, meaning it’s raining, and isSunny
is set to false
, meaning it’s not sunny.
2. Boolean Expressions:
Booleans often come up when you’re making decisions in your code. You use comparison operators like ==
(equal), !=
(not equal), <
(less than), >
(greater than), <=
(less than or equal to), and >=
(greater than or equal to) to create Boolean expressions:
let age = 15
let isTeenager = age >= 13 && age <= 19
Here, isTeenager
will be true
if age
is between 13 and 19, and false
otherwise.
3. Logical Operators:
You can combine multiple Boolean expressions using logical operators like &&
(AND), ||
(OR), and !
(NOT):
let isSunny : Bool = true
let isWarm : Bool = false
let isGoodWeather : Bool = isSunny && isWarm
Here, isGoodWeather is true only if both isSunny and isWarm are true.
Booleans are like the traffic signals in your code, helping you make decisions based on certain conditions. So, when you want to check if something is true or false, Booleans are your go-to!
In this Swift adventure, we’ve covered some basics to get you started in the exciting world of programming. We introduced Swift as a language created by Apple for iOS and macOS app development. With the help of Xcode, the integrated development environment, and the LLVM compiler, you can bring your Swift code to life.
We explored the concept of The timeless tradition of beginning with a “Hello World” example that showcased Swift’s simplicity. We even added a personal touch with string interpolation, making our messages more engaging. Numbers and their types, like integers and doubles, were next on the list, and we delved into the power of arithmetic operations. Strings, those versatile collections of characters, and their manipulation, along with the introduction of Booleans for decision-making, rounded up our initial journey. Stay tuned for Part 2, where we’ll delve deeper into Swift’s capabilities and explore more exciting features!
and this is my example tutorial on the GitHub repository Swift-Fundamentals. I will upload all of Swift Fundamental here.
Follow me on GitHub: MadhushaPrasad