Challenge B: Randomization

Write a protocol extension on Sequence named randomized() that will rearrange the elements in a somewhat random order. You can test out your implementation on an ordinary Array, which implements Sequence.

Hints:

  • Your method signature should be randomized() -> [Iterator.Element]. The type [Iterator.Element] is an array of whatever type the Sequence holds, such as String or Int, .
  • You can call the arc4random_uniform() method like this: arc4random_uniform(2) to randomly generate a 1 or 0 for your randomization algorithm.

We hope you learned a lot about Swift in this book — and had some fun in the process! Swift is filled with language features and programming paradigms, and we hope you now feel comfortable enough with the language to move on to building bigger things.

With the language fundamentals under your belt, you're ready to crack your knuckles with iOS apps, macOS apps, the latest killer tvOS app and who knows what else!

If you have any questions or comments as you continue to use Swift, please stop by our forums at http://www.raywenderlich.com/forums.

Thank you again for purchasing this book. Your continued support is what makes the tutorials, books, videos, conferences and other things we do at raywenderlich.com possible — we truly appreciate it!

Wishing you all the best in your continued Swift adventures,

– Matt, Erik, Eli, Ben, Alexis, Janie, Cosmin, Steven, Chris and Ray F. The Swift Apprentice team

results matching ""

    No results matching ""