Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learning Swift
  • Table Of Contents Toc
  • Feedback & Rating feedback
Learning Swift

Learning Swift

By : Andrew J Wagner
4.8 (5)
close
close
Learning Swift

Learning Swift

4.8 (5)
By: Andrew J Wagner

Overview of this book

If you are looking to build iOS or OS X apps using the most modern technology, this book is ideal for you. You will find this book especially useful if you are new to programming or if you have yet to develop for iOS or OS X.
Table of Contents (13 chapters)
close
close
12
Index

Putting protocols and generics to use


One cool thing about Swift is that it contains generators and sequences. They provide an easy way to iterate over a list of values. Ultimately, they boil down to two different protocols: GeneratorType and SequenceType. If you implement the SequenceType protocol in your custom types, it allows you to use the for-in loop over an instance of your type. In this section, we will look at how to do that.

Generators

The most critical part of this is the GeneratorType protocol. Essentially, a generator is an object that you can use repeatedly to ask for the next object in a series until there are no objects left. Most of the time, you can simply use an array for this, but it is not always the best solution. For example, you can even make a generator that is infinite.

There is a famous infinite series of numbers called the Fibonacci Sequence where every number in the series is the sum of the two previous numbers. This is especially famous because it is found everywhere...

Unlock full access

Continue reading for free

A Packt free trial gives you instant online access to our library of over 7000 practical eBooks and videos, constantly updated with the latest in tech
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY