Swift and How It Is Used to Make iOS Apps
Published: 2024-06-10 14:44:24

As a student who is very interested in IOS app development, it can get very overwhelming with all the different language options available. Among these, Swift stands out for its simplicity, safety, and user-friendliness. In this blog post, we'll explore three key concepts in Swift that makes it the best option for ios development compared to JavaScript to provide a perspective from a beginner who is looking for the best language to use.

Swift's Safety and Type System
One of the most appealing features of Swift is its focus on safety. Swift is a type-safe language, which means it ensures that your code doesn’t mix values; such as numbers and strings. This safety feature helps prevent type-related errors and makes your code more predictable and dependable. In contrast, JavaScript will allow you to have mixed values in one declared variable. Such as an array, javascript will allow you to try to add a number with a string which will the output be a NAN, so far as with swift, it will auto correct. It will change the outlier to the corrected value.

// Swift can infer types, so you can also write:
let inferredNumbers = [1, 2, 3, 4, 5] // inferred as [Interger]
let inferredSum = inferredNumbers.reduce(0, +) // inferred as Int and sums up to 15
// However, JavaScript's dynamic typing can lead to unexpected results:
 let mixedArray = [1, '2', 3, 4, 5];
 let mixedSum = mixedArray.reduce((acc, num) => acc + num, 0); // Results in "012345" due to string concatenation

In the above example, Swift's type safety assures you that an array of numbers contains only integers, preventing type errors. In JavaScript it allows mixed values within arrays, which can lead to unexpected results or errors instead of numerical addition, unlike Swift. In this case, swift is way more dependable. Imagine not having to face any runtime delay errors. This Swift tool allows you to code more confidently and more efficiently.
Optionals is another powerful feature or tool in Swift that allows you to represent a missing value. This is useful when dealing with scenarios where a value is missing or not declared. JavaScript, on the other hand, uses null and undefined to represent the absence of a value, which can sometimes lead to errors or confusion. In Swift, you declare an optional by using a ? to the type. This means the variable can hold either a value of the specified type or ‘nil’ which is equal to no value.
// Swift Example
var optionalName: String? = "John"
optionalName = nil // valid because optionalName can hold a String or nil
// Unwrapping an optional safely using if-let
if let name = optionalName {
    print("Hello, \(name)")
} else {
    print("optionalName is nil")
}
In this example optionalName is declared as an optional String, so it can either hold a String value or be nil. Using the if let syntax, we safely unwrap optionalName. If it contains a value, we print it. Otherwise, we handle the case where it is nil.
Now lets compare it to a javascript example so you could see the beauty of optionals and how it can save you time and how efficient it is. In JavaScript, you can use null or undefined to represent the absence of a value. However, you need to check for these values explicitly.
// JavaScript Example
let name = "John";
name = null; // Can also be undefined
// Checking for null or undefined
if (name !== null && name !== undefined) {
    console.log(`Hello, ${name}`);
} else {
    console.log("name is null or undefined");
}
As you can see Swift's optionals provide a clear and safe way to handle missing values, reducing the likelihood of runtime errors. JavaScript’s approach requires more precise checks to ensure that a value is neither null nor undefined before using it.
2. Swift’s Visual and Interactive Toolsx
Swift's syntax is designed to be expressive and easy to read, making it an excellent choice for beginners. Its modern syntax makes common programming tasks easy to complete, especially in gaming development. Swift offers tools and frameworks for visualizing and developing games, particularly through Xcode and the SpriteKit framework. Swift Playgrounds is an interactive environment provided by Apple that makes learning Swift and experimenting with code fun and engaging. It’s particularly useful for visual learners and those new to programming. SpriteKit is a powerful framework provided by Apple specifically for 2D game development. It simplifies many aspects of game development, from rendering graphics to handling physics and user interactions. JavaScript also supports game development, particularly through frameworks like Phaser and libraries like Three.js for 3D graphics. However, the development environment is different and often less integrated than Swift's tools in Xcode and Playgrounds. 

Swift’s tools like Playgrounds and frameworks like SpriteKit offer a highly integrated and user-friendly environment for game development. This makes Swift particularly appealing for beginners and visual learners, providing immediate feedback and simplifying the process of creating interactive and graphical applications. JavaScript, while also powerful for game development, often involves more setup and configuration, making Swift a more first choice option for many developers, specifically beginners.
3. Swift's User-Friendly Features
Swift was designed to be user-friendly, making it an excellent choice for beginners in iOS development. Its clear syntax, powerful libraries, and interactive environment make learning and using Swift a smooth experience, its not meant to be a difficult language at all. Also, Swift comes with a wide varitety of libraries and frameworks that simplify many common tasks. The Cocoa and Cocoa Touch frameworks, for instance, provide a wide range of functionalities for developing macOS and iOS applications. JavaScript also has libraries and frameworks, but the integration and consistency of Swift’s libraries with Apple's ecosystem provide a more seamless development experience. At the same time Swift’s standard library is extensive and powerful, providing a wide range of functionalities. Additionally, Swift has several frameworks that make developing for iOS and macOS efficient and enjoyable.

let fruits = ["Apple", "Banana", "Cherry"]
let uppercaseFruits = fruits.map { $0.uppercased() }
print(uppercaseFruits) // ["APPLE", "BANANA", "CHERRY"]

In this example, the map function is part of Swift’s standard library and allows you to transform each element in an array in a concise and readable manner.

Swift’s focus on safety, modern syntax, and user-friendly features makes it an excellent choice for iOS development. Comparing it to JavaScript highlights Swift's advantages in type safety, syntax, and learning tools, while also showing JavaScript’s flexibility and widespread use in web development. As I continue learning and experimenting with Swift and JavaScript, I’ll appreciate the strengths and trade-offs of each language, helping me become a more versatile developer. But, I think if i really deep dive into swift i’ll actually fall in love with it. My real goal is to do freelance work for business and create them ios apps.