Mastering Swift Package Manager: Build and Test Modular Apps Using Xcode
By Avi Tsadok
()
About this ebook
Swift Package Manager isn’t magic, but you’ll likely need a little guidance in using this nifty tool. You'll see how to reorganize projects smartly and efficiently, create your first Swift package, and learn what libraries are in this context. Then share your package with the rest of your projects. Next test your library’s quality. And then take your library to the next level of power by including files that go beyond just code.
By going over the methodology of big project organization, you will have the ability to extend your project into the future as Apple sees it. Make no mistake—the path for a modular and flexible project is not easy. With the help of Swift Package Manager, it becomes possible and worth it.
What You'll Learn
- Create a Swift package both from the command line and from Xcode
- Manage Swift packages in public and private Git repositories
- Build test suites to test integrations between packages
Who This Book Is For
Senior iOS developers and team leaders who already have professional experience with Swift.
Read more from Avi Tsadok
The Ultimate iOS Interview Playbook: Conquer Swift, frameworks, design patterns, and app architecture for your dream job Rating: 0 out of 5 stars0 ratingsMastering iOS 18 Development: Take your iOS development experience to the next level with iOS, Xcode, Swift, and SwiftUI Rating: 0 out of 5 stars0 ratingsPro iOS Testing: XCTest Framework for UI and Unit Testing Rating: 0 out of 5 stars0 ratings
Related to Mastering Swift Package Manager
Related ebooks
Test-Driven iOS Development with Swift Rating: 0 out of 5 stars0 ratingsTest-Driven iOS Development with Swift 4 - Third Edition: Write Swift code that is maintainable, flexible, and easily extensible Rating: 0 out of 5 stars0 ratingsTest-Driven iOS Development with Swift 3 Rating: 0 out of 5 stars0 ratingsUI Design for iOS App Development: Using SwiftUI Rating: 0 out of 5 stars0 ratingsAn iOS Developer's Guide to SwiftUI: Design and build beautiful apps quickly and easily with minimum code Rating: 0 out of 5 stars0 ratingsTest-Driven iOS Development with Swift: Write maintainable, flexible, and extensible code using the power of TDD with Swift 5.5 Rating: 0 out of 5 stars0 ratingsSwift 5 for iOS and macOS Rating: 0 out of 5 stars0 ratingsCreating iOS apps with Xcode: Learn how to develop your own app (English Edition) Rating: 0 out of 5 stars0 ratingsIntroducing Jakarta EE CDI: Contexts and Dependency Injection for Enterprise Java Development Rating: 0 out of 5 stars0 ratingsBeginning IntelliJ IDEA: Integrated Development Environment for Java Programming Rating: 0 out of 5 stars0 ratingsHands-On Python for DevOps: Leverage Python's native libraries to streamline your workflow and save time with automation Rating: 0 out of 5 stars0 ratingsSwift 5 for Absolute Beginners: Learn to Develop Apps for iOS Rating: 0 out of 5 stars0 ratingsProtocol Buffers Handbook: Getting deeper into Protobuf internals and its usage Rating: 0 out of 5 stars0 ratingsASP.NET Core and Vue.js: Build real-world, scalable, full-stack applications using Vue.js 3, TypeScript, .NET 5, and Azure Rating: 0 out of 5 stars0 ratingsFull Stack iOS Development with Swift and Vapor: Full stack iOS development made easy (English Edition) Rating: 0 out of 5 stars0 ratingsGeneric Pipelines Using Docker: The DevOps Guide to Building Reusable, Platform Agnostic CI/CD Frameworks Rating: 0 out of 5 stars0 ratingsSwiftUI for Absolute Beginners: Program Controls and Views for iPhone, iPad, and Mac Apps Rating: 0 out of 5 stars0 ratingsSwift Programming Nuts and bolts Rating: 0 out of 5 stars0 ratingsiOS Programming Nuts and bolts Rating: 4 out of 5 stars4/5LLVM Essentials Rating: 1 out of 5 stars1/5iOS 15 Programming for Beginners: Kickstart your mobile app development journey by building iOS apps with Swift 5.5 and Xcode 13 Rating: 0 out of 5 stars0 ratingsSpring Boot and Angular: Hands-on full stack web development with Java, Spring, and Angular Rating: 0 out of 5 stars0 ratingsSpring 5.0 By Example: Grasp the fundamentals of Spring 5.0 to build modern, robust, and scalable Java applications Rating: 0 out of 5 stars0 ratingsDevSecOps for .NET Core: Securing Modern Software Applications Rating: 0 out of 5 stars0 ratingsImplementing C# 11 and .NET 7.0: Learn how to build cross-platform apps with .NET Core (English Edition) Rating: 0 out of 5 stars0 ratingsiOS 15 Application Development for Beginners: Learn Swift Programming and Build iPhone Apps with SwiftUI and Xcode 13 Rating: 0 out of 5 stars0 ratingsCloud-Native Development and Migration to Jakarta EE: Transform your legacy Java EE project into a cloud-native application Rating: 0 out of 5 stars0 ratingsVue CLI 3 Quick Start Guide: Build and maintain Vue.js applications quickly with the standard CLI Rating: 0 out of 5 stars0 ratingsGetting to Know Vue.js: Learn to Build Single Page Applications in Vue from Scratch Rating: 0 out of 5 stars0 ratings
Programming For You
Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsHTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Unreal Engine from Zero to Proficiency (Foundations): Unreal Engine from Zero to Proficiency, #1 Rating: 3 out of 5 stars3/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsLinux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Spies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5
Reviews for Mastering Swift Package Manager
0 ratings0 reviews
Book preview
Mastering Swift Package Manager - Avi Tsadok
© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
A. TsadokMastering Swift Package Managerhttps://doi.org/10.1007/978-1-4842-7049-3_1
1. Introduction
Avi Tsadok¹
(1)
Tel Mond, Israel
Programming without an overall architecture or design in mind is like exploring a cave with only a flashlight: You don’t know where you’ve been, you don’t know where you’re going, and you don’t know quite where you are.
—Danny Thorpe
If you are reading this book, you are probably an iOS developer already. This is not a book for beginners or developers who never touched Xcode in their lives.
And if you did build an app or two, you already worked with frameworks and modules.
In fact, you are using frameworks and libraries every day, even if you are not aware of them.
Foundation, UIKit, Core Data, SwiftUI, StoreKit, and many more are just a few examples of frameworks we use every day.
Apple has over 120 frameworks and libraries in their SDK, and this number is growing with every iOS version released.
Frameworks and libraries are a great way to share and bundle different technologies or services.
Sometimes, when talking about third-party services, it’s the main and only way to do that.
But frameworks/libraries/modules are not just for Apple to provide us development tools or for companies to share their API with us.
We, as developers, can make use of libraries in our apps and projects.
Separating our apps into modules gives us a different aspect of how projects are built and the internal relationships between them.
Personal View
Implementing Swift Packages in my projects was a personal revolution I had in my development career.
The technical details on how to do that are not important – you can find them in every blog, manual, or Apple developer website.
But instead of looking at my project as an extensive set of classes with unclear relationships, I started to look at it as a group of technologies with clear responsibilities and a defined API.
Each module can be shared, tested, and improved over time.
And for me, it wasn’t just a minor change – it was a giant leap forward.
My project improved in every level – it was more stable, more testable, and even faster.
It’s not that Swift Packages have a secret feature that makes your project run better.
The process of creating a Swift Package forces you to take good and healthy steps that eventually improve your projects, such as testing, API design, dependency injection, documentation writing, and many more.
Once I did that, I realized how important it is to modularize my code, be organized, and work with API-driven
design.
The Future by Apple
Building modular apps was always possible in iOS and macOS. We have frameworks and static and dynamic libraries. But in the end, the key is how you manage your dependencies efficiently.
Over the years, Apple focused on driving the MVC pattern forward.
But in the last 2–3 years, things have changed, and we can see the new direction Apple is taking us:
SwiftUI – SwiftUI is Apple’s declarative framework for building user interfaces, based on some Swift language features and ideas taken from other declarative frameworks such as React and Flutter.
With SwiftUI, Apple is, in fact, dumping MVC. So, what is the new design pattern that we’re using here? We don’t have a controller, but we know we have a view and a model. The view is binding to the model through an observable object which acts as a view model. As a result, we can say that the classic SwiftUI implementation is much like MVVM.
What’s also interesting is that SwiftUI is how Apple wants us to build in all of its platforms, not only iOS.
For Apple, SwiftUI is the future.
Swift Package Manager – When Apple introduced Swift Package Manager, we could create a new Swift Package using the command line only. But starting Xcode 11, creating a new Swift Package is built into Xcode, including dependency configuration. We see further improvements in Xcode 12 that allow us to add resources and files to a package and make it much more powerful, almost like a complete framework.
We can conclude that Apple sees Swift Packages as the basic logic unit of all of our apps.
Combine – Once we have a declarative UI framework (SwiftUI) on the one hand, and on the other hand a set of libraries, it is just natural to manage the data flow with Combine. Combine is Apple’s version for RxSwift, a reactive framework that can help you handle process values over time, and it was introduced along with SwiftUI in WWDC 2019.
It is the glue that connects the business logic to the UI layer.
So, if we put it all together, we’ll see something like Figure 1-1.
../images/505703_1_En_1_Chapter/505703_1_En_1_Fig1_HTML.jpgFigure 1-1
How Combine, SwiftUI, and Packages go together
If we want our projects to fit in with how Apple sees the future of development, we now understand that Swift Packages play a significant role and how important it is to start and change our mindset on how our apps are built.
About This Book
This book is not a manual or documentation.
I was so excited about Swift Package’s changes to my projects, so I wanted to explore that area even more profoundly – what it means about architecture, code coupling, API design, and more.
And while I was scratching more and more layers, I realized that the thing called Swift Package Manager
is much more than a technical project component – it’s a whole new perspective.
You will find here how to create and maintain Swift Packages both from Xcode and Terminal, at the deepest level possible, and how to do it right. And this is what this book is all about.
Even though you can read this book in whatever order you wish, reading it chronologically is the recommended way to go – from theory and design to basic Swift Package maintenance, distributing, and finishing with testing and resources management.
Throughout the book, you will meet the iOS development team of Weathery Apps
– an imaginary company that struggles to improve their app using Swift Package Manager for the first time.
In each chapter, the team encounters a real-world problem and solves it using different Swift Package Manager features or aspects that will be widely discussed.
Enjoy the reading!
© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
A. TsadokMastering Swift Package Managerhttps://doi.org/10.1007/978-1-4842-7049-3_2
2. Organized Mess
Avi Tsadok¹
(1)
Tel Mond, Israel
Every program attempts to expand until it can read mail. Those programs which cannot so expand are replaced by ones which can.
—Jamie Zawinski
Before we run forward and talk about Swift Packages, frameworks, and libraries, we need to prepare our app for that.
In the bottom line, packages are just another technical expression of good architecture and reusable code. It is almost inevitable to organize your project if you want to implement Swift Packages.
In this chapter, you will
Meet Weathery,
an imaginary project that we follow in this book
Learn what the separation of concerns principle is when talking about architecture
Learn what the three-layer architecture is and what its benefits are
Understand how to redesign our app by using a UML
Learn what closed/opened layers, design patterns, entities, and interfaces are
Meet Weathery
It was a lovely day at Weather Apps,
the company behind Weathery,
the App Store’s best weather app.
Weathery
is a long-existing app and has millions of downloads.
Even though a weather app sounds like a small project, it has a varied list of features, such as
Authentication flow, with registration, login, and forgot password feature
A search engine
Location-aware features
In-app purchase
Beautiful screens that show weather in various places in the world
A user configuration page
Analytics
Other than that, the Weathery
iOS team is adding more features and bug fixes daily, trying to keep the app stable and agile and keep pace with its competitors.
Emily, can you come for a second? I need to talk with you about the direction we want to take
– a sound was heard in the open space out of the VP Product mouth.
Emily was the iOS tech lead of the app for the last 3 years. She knew every single line of code and was in charge of the most advanced features the app has.
We see an increased demand for an Apple Watch version . Many users ask for that. We want to take the current version and build a dedicated Apple Watch version. The plan is to do the same for macOS in the future, but as I said, we want to start with the Watch first. What do you say? How easy is it? We already have the logic written, don’t we?
The Weathery Project
To understand if Emily can try to use the already written logic for the Apple Watch, we need to examine the Weathery project. Let’s have a look at the project navigator (Figure 2-1).
../images/505703_1_En_2_Chapter/505703_1_En_2_Fig1_HTML.jpgFigure 2-1
Weathery
project navigator
By quickly going over the existing folders, we can see a few things:
The iOS team organized some of the files by their operational part. We can see a dedicated folder for view controllers, another one for views, and so on.
Regardless of the previous point, we can also see different services – we see folders for a network, analytics, and persistent store.
Since we don’t see any business logic folders, we assume the view controllers hold most of the app’s logic.
This may look like a big mess, but we do see a pattern here. The Weathery team organized (Figure 2-2) their code.
../images/505703_1_En_2_Chapter/505703_1_En_2_Fig2_HTML.jpgFigure 2-2
Weathery project organization
This may be an intuitive way to organize files, but not always the efficient one. Regarding the new mission to develop an Apple Watch version, managing your project can make it challenging to maintain it over time. Emily needs to rethink about reorganizing its project in a different way, a more efficient one.
Code Organization
We can’t say the Weathery project code is not organized, but we indeed have a better way to manage our code and make it more reusable and stable. To reveal how to do that, we need to understand a few essential principles, some of them are probably familiar to you.
Note
Right now, I don’t want to talk about any technical terms such as frameworks, libraries, and modules. We need to understand the theory first.
Separation of Concerns
Separation of concerns (SoC) is one of the most crucial principles when talking about software design. SoC means two things:
Each component, a function, or a class has only one responsibility.
Two components or two functions cannot share the same responsibility.
Separation of concerns is a key component when talking about a modular app. We already know we should write small functions that their goal is to do one task only. We also realize that’s the case with classes. We don’t always understand that this principle is also valid in a full module or a library.
One of the best ways to assimilate this is to move out of the computer science world and go to real life.