Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Swift

You're reading from   Learning Swift Build a solid foundation in Swift to develop smart and robust iOS and OS X applications

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher Packt
ISBN-13 9781784392505
Length 266 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Andrew J Wagner Andrew J Wagner
Author Profile Icon Andrew J Wagner
Andrew J Wagner
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introducing Swift 2. Building Blocks – Variables, Collections, and Flow Control FREE CHAPTER 3. One Piece at a Time – Types, Scopes, and Projects 4. To Be or Not to Be – Optionals 5. A Modern Paradigm – Closures and Functional Programming 6. Make Swift Work for You – Protocols and Generics 7. Everything is Connected – Memory Management 8. Writing Code the Swift Way – Design Patterns and Techniques 9. Harnessing the Past – Understanding and Translating Objective-C 10. A Whole New World – Developing an App 11. What's Next? Resources, Advice, and Next Steps Index

Refactoring to respect Model-View-Controller

We already made some good progress on the core functionality of our app. However, before we move any further, we should reflect on the code we have written. Ultimately, we haven't actually written that many lines of code, but we can definitely improve on this. The biggest shortcoming of our code is that we have put a lot of business logic inside our view controller. This is not a good separation of our different model, view, and controller layers. Let's take this opportunity to refactor this code into a separate type.

We will create a class called PhotoStore that will be responsible for storing our photos and that will implement the data source protocol. This means that we have to move some of our code out of our view controller, which is exactly our goal.

To do this, first, we will move the photos property to the PhotoStore class:

import UIKit

class PhotoStore: NSObject {
    var photos = [Photo]()
}

Note that this new photo store class...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image