Swift Tutorials: Array of Custom Types 🎯

beginner
21 min

Swift Tutorials: Array of Custom Types 🎯

Welcome to our deep dive into Swift's world of custom types and arrays! In this lesson, we'll learn how to create and manipulate arrays of custom types. By the end of this tutorial, you'll have a solid understanding of how to work with custom data structures, a crucial skill for any Swift developer. Let's get started! 📝

Custom Structures and Classes 💡

Before we dive into arrays, let's quickly review Swift's two primary custom data types: structures (struct) and classes (class). Both are used to create complex data types, but they have some differences.

swift
struct Point { var x: Double var y: Double } class Rectangle { var width: Double var height: Double }

In the examples above, we have a Point structure and a Rectangle class. Both contain properties x, y, width, and height, but classes are more flexible and can include methods, inheritance, and other advanced features. 📝

Creating and Using Arrays of Custom Types 💡

Now that we have our custom structures and classes, let's see how to create arrays of them!

swift
var points: [Point] = [] var rectangles: [Rectangle] = []

In the code above, we've created an empty array of Point and Rectangle types. Now, let's populate these arrays with some data.

swift
let origin = Point(x: 0, y: 0) let pointA = Point(x: 1, y: 1) let pointB = Point(x: 2, y: 2) points.append(origin) points.append(pointA) points.append(pointB) let rectangle1 = Rectangle(width: 5, height: 10) let rectangle2 = Rectangle(width: 10, height: 5) rectangles.append(rectangle1) rectangles.append(rectangle2)

In this example, we've created some Point and Rectangle instances, added them to our arrays, and stored them in variables for easier access. 💡

Accessing and Manipulating Array Elements 💡

Now that we have our arrays populated, let's learn how to access and manipulate their elements.

swift
print(points[0]) // Prints: (0, 0) print(rectangles[0].width) // Prints: 5 points[1].x += 2 print(points[1]) // Prints: (3, 1)

In the example above, we're accessing the first element of each array, modifying a Point's x property, and printing the updated values. 💡

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the output of the following code?

Advanced Array Manipulation with Loops and Methods 💡

In this section, we'll learn how to iterate through arrays using loops and define custom methods for our custom types.

swift
for point in points { print("(point.x), (point.y)") } func perimeter(rectangle: Rectangle) -> Double { return 2 * (rectangle.width + rectangle.height) } for rectangle in rectangles { print("Perimeter: (perimeter(rectangle))") }

In the example above, we're iterating through the points array and printing the x and y coordinates of each point. Additionally, we've defined a perimeter method for Rectangle and calculated the perimeter of each rectangle in the rectangles array. 💡

Creating Custom Types with Initializers and Conformances 💡

In our final section, we'll learn about initializers and conformance to protocols to create more powerful custom types.

swift
struct Circle { var radius: Double init(radius: Double) { self.radius = radius } } protocol AreaCalculatable { var width: Double { get } var height: Double { get } var area: Double { get } } extension Circle: AreaCalculatable { var area: Double { π * radius * radius } } let circle = Circle(radius: 5) print("Area: (circle.area)")

In the example above, we've created a Circle structure with an initializer, and conformed it to the AreaCalculatable protocol. We've also defined an extension for Circle that calculates the area based on the radius. 💡

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the output of the following code?

That's it for our Swift tutorial on arrays of custom types! We've covered the basics of creating and using arrays, manipulating array elements, looping through arrays, defining custom initializers, and conforming to protocols. With this knowledge, you're well on your way to mastering Swift's powerful data structures. ✅

Happy coding, and keep exploring the world of Swift with CodeYourCraft! 🎯