7 Key Strategies for Reducing Code Duplication in SwiftUI

Experienced iOS and React Native Developer with 10+ Years of Expertise - Available for New Opportunities. Connect Now!
Search for a command to run...

Experienced iOS and React Native Developer with 10+ Years of Expertise - Available for New Opportunities. Connect Now!
No comments yet. Be the first to comment.
This post will guide you through the process of converting between Swift and SwiftUI types and vice versa. This ability is quite useful, especially when you have existing Swift code that you want to integrate with SwiftUI or need to use SwiftUI types...
This time, I'm exploring the fascinating world of AI, stepping away from my usual focus on iOS development. There's so much more to share about AI, and I can't wait to dive into it with you! If you'd like to follow along and receive regular updates, ...

The app is Time Tango, a Pomodoro timer app. I chose this concept after conducting keyword research for popular terms that face minimal competition on the app store. The app is free to use and received approval in its first review. The strategy focus...

In my last article, I talked about how data flows in SwiftUI. Now, Apple has released something new in iOS 17 - the Observation Framework. This changes how SwiftUI works with data. In this article, we will look at how to use the new Observable Macro ...

One of the critical elements of SwiftUI is its approach to managing data flow within applications. This comprehensive guide will provide you with a strong understanding of data flow in SwiftUI, how it operates, and why it's vital for your SwiftUI app...

Arrays are one of the most common data structures used in programming. In Swift, arrays are used to store ordered lists of values of the same type. They are incredibly versatile and powerful, but using them efficiently is key to writing high-performi...

As software developers, we're often told to follow the DRY (Don't Repeat Yourself) principle, which is aimed at reducing the repetition of software patterns. SwiftUI provides various tools and methodologies that aid us in adhering to this principle. This blog post will cover a number of these techniques, complete with code examples.
Extracting views is one of the simplest ways to avoid code duplication. This allows us to create reusable components that can be used throughout the app. For example, if you find yourself creating a similar Button style across multiple screens, consider extracting it into a separate View.
struct CustomButton: View {
var title: String
var action: () -> Void
var body: some View {
Button(action: action) {
Text(title)
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
}
}
You can now use CustomButton throughout your SwiftUI code, ensuring a consistent look and feel across your app.
ViewModifiers provide an elegant way to bundle view modifications for reuse. This can dramatically reduce code duplication, particularly when dealing with consistent styling across many views.
struct CustomTextStyle: ViewModifier {
func body(content: Content) -> some View {
content
.font(.title)
.foregroundColor(.blue)
}
}
extension View {
func customTextStyle() -> some View {
self.modifier(CustomTextStyle())
}
}
Now you can apply customTextStyle() to any SwiftUI View.
Creating reusable components is a powerful way to minimize code duplication. This could include buttons, form fields, cards, or any other UI element that appears frequently in your app. Here's an example of a reusable card view:
struct CardView: View {
var body: some View {
VStack {
Image("example-image")
.resizable()
.aspectRatio(contentMode: .fit)
Text("Example Text")
.font(.headline)
.padding()
}
.frame(width: 300, height: 400)
.background(Color.white)
.cornerRadius(10)
.shadow(radius: 10)
}
}
This CardView can be used anywhere in your app, reducing the need to repeat similar code.
ViewBuilder is a result builder that constructs a view from a series of expressions. You can create functions that return complex views, helping you avoid repetition.
@ViewBuilder
func customLabel(title: String, subtitle: String) -> some View {
VStack(alignment: .leading) {
Text(title)
.font(.title)
Text(subtitle)
.font(.subheadline)
.foregroundColor(.gray)
}
}
This customLabel function can be used to create a title and subtitle pair, reducing the need for duplication.
Swift extensions allow you to add new functionality to existing types, which can be very useful in SwiftUI. For example, you can add a custom modifier to all Text views in your app, reducing the need to apply the same modifiers to Text views repeatedly.
extension Text {
func titleStyle() -> some View {
self
.font(.largeTitle)
.foregroundColor(.primary)
}
}
Moving logic into model objects can significantly reduce code duplication. SwiftUI's design encourages a clear separation of concerns, with views focusing on UI and model objects handling data and business logic.
class TaskViewModel: ObservableObject {
@Published var tasks = [Task]()
func loadTasks() {
// Load tasks from a data source
}
func addTask(_ task: Task) {
tasks.append(task)
}
}
This TaskViewModel can be used across multiple views, reducing the need to duplicate task management code.
SwiftUI provides several pre-built views such as List, Form, Picker, and more. By leveraging these, you can reduce the amount of custom code you need to write.
struct TaskListView: View {
var tasks = ["Task 1", "Task 2", "Task 3"]
var body: some View {
List(tasks, id: \.self) { task in
Text(task)
}
}
}
In this TaskListView, we use SwiftUI's List view to display a list of tasks. This eliminates the need to manually create and manage a list of views.
These tips should help you keep your SwiftUI codebase DRY and maintainable. Remember, the goal of reducing code duplication is to make the code easier to maintain, read, and debug. Always keep this in mind when making decisions about how to structure your SwiftUI code.
You may also like: SwiftUI Magic: 7 Lesser-Known Features That Will Make Your Apps Stand Out
I hope you enjoyed this article, and if you have any questions, comments, or feedback, then feel free to comment here or reach out via Twitter.
Thanks for reading!