From the course: Swift: Delegations and Data Sources

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Data sources

Data sources

From the course: Swift: Delegations and Data Sources

Start my 1-month free trial

Data sources

- [Instructor] So far, we've used protocols for delegates. There's one other common use for protocols that's related to delegates called data sources. Even if you never implement a data source, you will be using them all the time, so it's important to understand them. Data sources solve another problem with encapsulation and inaccessible properties. Go to my playground and the color chooser. And let's go up to our model here of colors, and I want to talk about a few things here. Let's take a look again. Let's suppose that instead of a let we have a var here for the color names. And instead of private we have var here. So these are two perfectly big and normal properties. There's dozens of ways, given the code we've got, that anyone setting these methods could crash this class. Now, another developer could change the data tapes on you, such as turn a UI color into a string, or have a mismatch in the sizes of the arrays. So how do I hold on to the data integrity? Now, for some…

Contents