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! 📝
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.
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. 📝
Now that we have our custom structures and classes, let's see how to create arrays of them!
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.
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. 💡
Now that we have our arrays populated, let's learn how to access and manipulate their elements.
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. 💡
What is the output of the following code?
In this section, we'll learn how to iterate through arrays using loops and define custom methods for our custom types.
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. 💡
In our final section, we'll learn about initializers and conformance to protocols to create more powerful custom types.
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. 💡
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! 🎯