Book Image

iOS Programming Cookbook

Book Image

iOS Programming Cookbook

Overview of this book

Do you want to understand all the facets of iOS programming and build complex iOS apps? Then you have come to the right place. This problem-solution guide will help you to eliminate expensive learning curves and focus on specific issues to make you proficient at tasks and the speed-up time involved. Beginning with some advanced UI components such as Stack Views and UICollectionView, you will gradually move on to building an interface efficiently. You will work through adding gesture recognizer and touch elements on table cells for custom actions. You will work with the Photos framework to access and manipulate photos. You will then prepare your app for multitasking and write responsive and highly efficient apps. Next, you will integrate maps and core location services while making your app more secure through various encryption methods. Finally, you will dive deep into the advanced techniques of implementing notifications while working with memory management and optimizing the performance of your apps. By the end of the book, you will master most of the latest iOS 10 frameworks.
Table of Contents (22 chapters)
iOS Programming Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Using Operation subclassing


Most often, operations that you need to perform are better encapsulated in a custom subclass of the Operation class. We have already worked with BlockOperation in the previous demo, but we saw a lot of redundancy in writing code and it's not customized. In this section, we will implement the same demo and see how we build a custom Operation class to perform the task that will be done concurrently.

How to do it...

To build a custom Operation class, perform the following steps:

  1. In our Xcode project, add a new Swift file with a class, named ImageDownloader, which extends the Operation class:

  2. In the ImageDownloader.swift file, add the following code:

          class ImageDownloader: Operation { 
              let imgURL: URL 
              var downloadedImage: UIImage? 
              init(imageURL: URL) { 
                  self.imgURL = imageURL 
              } 
         
              override func main() { 
                  if self.isCancelled { 
         ...