Creating ImageStore

In Chapter 15, you will have instances of Item write out their properties to a file, which will then be read in when the application starts. However, because images tend to be very large, it is a good idea to keep them separate from other data. You are going to store the pictures the user takes in an instance of a class named ImageStore. The image store will fetch and cache the images as they are needed. It will also be able to flush the cache if the device runs low on memory.

Create a new Swift file named ImageStore. In ImageStore.swift, define the ImageStore class and add a property that is an instance of NSCache.

import Foundation
import UIKit

class ImageStore {

    let cache = NSCache()

}

The cache works very ...

Get iOS Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.