Swift Package Collections

by @ralfebert · updated July 17, 2021
Xcode 13 & iOS 15
Advanced iOS Developers

With Package Collections you can quickly look up and add a Swift Package to your project in Xcode 13. Let's use the new Swift Algorithms package as an example - this package provides implementations for algorithms often needed for everyday programming tasks. Adding such a package is now only two clicks away:

Task

Display a list of food items grouped in chunks:

Required knowledge

You need basic knowledge of Swift and SwiftUI and you need to know how to → use Swift Packages in Xcode projects.

Steps

  1. Make sure you're using Xcode 13, this example uses new features in Xcode 13 (this was last tested with Xcode 13 beta 3).

  2. Download and open the starter project for the example project: FoodChoiceExample.

  3. In FoodChoiceView, manually write an import for the Algorithms package. Build the project to get a compiler error which offers to add the package to the project for you:

    Get the swift-algorithms package from the package collection - Xcode comes pre-configured with a Package collection with the packages from Apple:

  4. Add a VStack / ForEach to the view and use the method chunks(ofCount:) from the Swift algorithm package to group the food items in chunks:

Additional Tasks

Discussion

More information