Swift Algorithms: Getting Started | raywenderlich.com

Learn about Apple’s open-source Swift Algorithms package, which can be used to simplify complex code and improve its performance.

This is a companion discussion topic for the original entry at https://www.raywenderlich.com/18517868-swift-algorithms-getting-started
1 Like

There is a typo in the comments for this code:

numberOfPermutations(4, setSize: 2) // 12
numberOfPermutations(6, setSize: 2) // 20
numberOfPermutations(6, setSize: 6) // 720

The number of permutations of size 2 from a domain of 6 elements is 30.

Got error after input code: let exampleCombinations = [“A”, “B”, “C”].combinations(ofCount: 2)
How to fix it?
XCode 12.4, Swift 5

error: Combinations.xcplaygroundpage:45:43: error: value of type ‘[String]’ has no member ‘combinations’
let exampleCombinations = [“A”, “B”, “C”].combinations(ofCount: 2)
~~~~~~~~~~~~~~~ ^~~~~~~~~~~~

Hello @eggmanqi, thank you for mentioning this right away.
I managed to reproduce this right away even on the “final” project with Xcode 12.4. I believe something changed in how linking works between the two. no idea what or why but anyway I managed to find a quick solution and I’ll update the getting started part right away to match.

After you add the algorithms Swift Package do the following:

Select the project file from the navigator, then select the target “UmbrellaFramework”. Select “Build Phases” and add the “Algorithms” package as a dependency.

In the Playground files remove the “import Algorithms” line at the top. this means you should have only ONE include not TWO.
In either of the two swift files in the project (“SampleData.swift” or “Utilities.swift”) add “import Algorithms”. This is very important. without it the dependency step won’t do much for the playground.
This will make it work and playgrounds will be happy.

If you keep the two includes it won’t work. which is odd imho.

Let me know if you managed to get it to work.

@mr_berna thank you so much for spotting it :]

I updated the Getting Started section to include steps for adding Algorithms as a dependency to the framework target. This should resolve thee linking problem I the playground.
I also updated the final project so its working right away without the need of any changes.

Updating my earlier comment: It doesn’t matter if the Algorithms is included in the playground page or not as long as it is present in the dependencies AND its inclusion in mentioned in one of the code files in the framework.

I hope this resolved the issue for everyone :]


Wooo it works now~ thanks update

1 Like

In the “Chunked” playground, there’s an omission in the “before” code. Ironically, it serves to illustrate the advantage of using Swift Algorithms: No more forgetting to flush the last result when constructing a variable from an iteration over a set!

Specifically, after “for (movie, year) in marvelMovieswithYears {…}”, you need to add a final “groupedMovies.append(currentGroup)”. Otherwise all those great movies from 2019 will be skipped. :slight_smile: