Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Mastering Swift Package Manager: Build and Test Modular Apps Using Xcode
Mastering Swift Package Manager: Build and Test Modular Apps Using Xcode
Mastering Swift Package Manager: Build and Test Modular Apps Using Xcode
Ebook289 pages2 hours

Mastering Swift Package Manager: Build and Test Modular Apps Using Xcode

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Maintaining an iOS project often results in thousands of code files scattered around your folders. Did you ever wanted to reuse those files with other projects but found yourself making copies of Swift files? You are not alone! Many iOS developers struggle with this issue as well—unorganized projects, duplicated files, difficulty in testing, and long complication times.  This book will show how Swift Package Manager can help you handle these problems by restructuring your project to make it efficient and straightforward. 
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.
LanguageEnglish
PublisherApress
Release dateMay 27, 2021
ISBN9781484270493
Mastering Swift Package Manager: Build and Test Modular Apps Using Xcode

Read more from Avi Tsadok

Related to Mastering Swift Package Manager

Related ebooks

Programming For You

View More

Related articles

Reviews for Mastering Swift Package Manager

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    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.jpg

    Figure 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.jpg

    Figure 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.jpg

    Figure 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.

    Enjoying the preview?
    Page 1 of 1