Welcome to the Swift Tutorials on CodeYourCraft! This lesson will introduce you to the powerful and modern programming language developed by Apple, called Swift. We'll explore Swift's unique features, benefits, and practical applications to help you get started on your coding journey.
Swift is a high-performance and open-source programming language used for developing iOS, macOS, watchOS, and tvOS apps. It's a clean, modern, and user-friendly language that combines the power of Objective-C and the simplicity of Python.
Swift has several basic data types, including:
Int: whole numbers (e.g., 1, 100, -5)Double: decimal numbers (e.g., 3.14, 0.0001)String: text (e.g., "Hello, World!")Bool: boolean values (true or false)Swift uses a clean syntax that's easy to read and write. Let's take a look at a simple Swift function:
func greet(person: String) -> String {
return "Hello, \(person)!"
}
// Calling the function
let name = "John"
let greeting = greet(person: name)
print(greeting) // Output: Hello, John!š” Pro Tip: Functions in Swift are defined using the func keyword. They can take parameters, perform actions, and return values.
Here are two complete, working examples to help you get started with Swift:
func calculateAverage(num1: Double, num2: Double) -> Double {
let average = (num1 + num2) / 2
return average
}
let num1 = 10.0
let num2 = 20.0
let average = calculateAverage(num1: num1, num2: num2)
print(average) // Output: 15.0func isEven(number: Int) -> Bool {
if number % 2 == 0 {
return true
} else {
return false
}
}
let number = 4
if isEven(number: number) {
print("\(number) is even.")
} else {
print("\(number) is odd.")
}
// Output: 4 is even.To help reinforce your understanding of Swift, let's take a short quiz:
Which Swift data type is used for whole numbers?
Now that you have a basic understanding of Swift, you're ready to explore more advanced topics. In the next lesson, we'll dive deeper into Swift's syntax and learn about control structures like loops and conditional statements.
Stay tuned for more Swift Tutorials on CodeYourCraft! šÆ