Creating and Using Keys

When an image is added to the store, it will be put into a dictionary under a unique key, and the associated BNRItem object will be given that key. When the BNRDetailViewController wants an image from the store, it will ask its item for the key and search the dictionary for the image. Add a property to BNRItem.h to store the key.

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​r​e​a​d​o​n​l​y​,​ ​s​t​r​o​n​g​)​ ​N​S​D​a​t​e​ ​*​d​a​t​e​C​r​e​a​t​e​d​;​

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​c​o​p​y​)​ ​N​S​S​t​r​i​n​g​ ​*​i​t​e​m​K​e​y​;​

The image keys need to be unique in order for your dictionary to work. While there are many ways to hack together a unique string, you are going to use the Cocoa Touch mechanism for creating universally unique identifiers (UUIDs), also known as globally unique identifiers (GUIDs). Objects of type NSUUID represent a UUID and are generated using the time, a counter, and a hardware identifier, which is usually the MAC address of the WiFi card. When represented as a string, UUIDs look something like this:

4​A​7​3​B​5​D​2​-​A​6​F​4​-​4​B​4​0​-​9​F​8​2​-​E​A​1​E​3​4​C​1​D​C​0​4​

Import BNRImageStore.h at the top of BNRDetailViewController.m.

#​i​m​p​o​r​t​ ​"​B​N​R​D​e​t​a​i​l​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​h​"​
#​i​m​p​o​r​t​ ​"​B​N​R​I​t​e​m​.​h​"​
#​i​m​p​o​r​t​ ​"​B​N​R​I​m​a​g​e​S​t​o​r​e​.​h​"​

In BNRItem.m, modify the designated initializer to generate a UUID and set it as the itemKey.

-​ ​(​i​n​s​t​a​n​c​e​t​y​p​e​)​i​n​i​t​W​i​t​h​I​t​e​m​N​a​m​e​:​(​N​S​S​t​r​i​n​g​ ​*​)​n​a​m​e​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​v​a​l​u​e​I​n​D​o​l​l​a​r​s​:​(​i​n​t​)​v​a​l​u​e​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​s​e​r​i​a​l​N​u​m​b​e​r​:​(​N​S​S​t​r​i​n​g​ ​*​)​s​N​u​m​b​e​r​
{​
 ​ ​ ​ ​/​/​ ​C​a​l​l​ ​t​h​e​ ​s​u​p​e​r​c​l​a​s​s​'​s​ ​d​e​s​i​g​n​a​t​e​d​ ​i​n​i​t​i​a​l​i​z​e​r​
 ​ ​ ​ ​s​e​l​f​ ​=​ ​[​s​u​p​e​r​ ​i​n​i​t​]​;​
 ​ ​ ​ ​/​/​ ​D​i​d​ ​t​h​e​ ​s​u​p​e​r​c​l​a​s​s​'​s​ ​d​e​s​i​g​n​a​t​e​d​ ​i​n​i​t​i​a​l​i​z​e​r​ ​s​u​c​c​e​e​d​?​
 ​ ​ ​ ​i​f​ ​(​s​e​l​f​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​G​i​v​e​ ​t​h​e​ ​i​n​s​t​a​n​c​e​ ​v​a​r​i​a​b​l​e​s​ ​i​n​i​t​i​a​l​ ​v​a​l​u​e​s​
 ​ ​ ​ ​ ​ ​ ​ ​_​i​t​e​m​N​a​m​e​ ​=​ ​n​a​m​e​;​
 ​ ​ ​ ​ ​ ​ ​ ​_​s​e​r​i​a​l​N​u​m​b​e​r​ ​=​ ​s​N​u​m​b​e​r​;​
 ​ ​ ​ ​ ​ ​ ​ ​_​v​a​l​u​e​I​n​D​o​l​l​a​r​s​ ​=​ ​v​a​l​u​e​;​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​s​e​t​ ​_​d​a​t​e​C​r​e​a​t​e​d​ ​t​o​ ​t​h​e​ ​c​u​r​r​e​n​t​ ​d​a​t​e​ ​a​n​d​ ​t​i​m​e​
 ​ ​ ​ ​ ​ ​ ​ ​_​d​a​t​e​C​r​e​a​t​e​d​ ​=​ ​[​[​N​S​D​a​t​e​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​ ​N​S​U​U​I​D​ ​o​b​j​e​c​t​ ​-​ ​a​n​d​ ​g​e​t​ ​i​t​s​ ​s​t​r​i​n​g​ ​r​e​p​r​e​s​e​n​t​a​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​U​U​I​D​ ​*​u​u​i​d​ ​=​ ​[​[​N​S​U​U​I​D​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​k​e​y​ ​=​ ​[​u​u​i​d​ ​U​U​I​D​S​t​r​i​n​g​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​_​i​t​e​m​K​e​y​ ​=​ ​k​e​y​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​/​/​ ​R​e​t​u​r​n​ ​t​h​e​ ​a​d​d​r​e​s​s​ ​o​f​ ​t​h​e​ ​n​e​w​l​y​ ​i​n​i​t​i​a​l​i​z​e​d​ ​o​b​j​e​c​t​
 ​ ​ ​ ​r​e​t​u​r​n​ ​s​e​l​f​;​
}​

Then, in BNRDetailViewController.m, update imagePickerController:didFinishPickingMediaWithInfo: to store the image in the BNRImageStore.

-​ ​(​v​o​i​d​)​i​m​a​g​e​P​i​c​k​e​r​C​o​n​t​r​o​l​l​e​r​:​(​U​I​I​m​a​g​e​P​i​c​k​e​r​C​o​n​t​r​o​l​l​e​r​ ​*​)​p​i​c​k​e​r​
d​i​d​F​i​n​i​s​h​P​i​c​k​i​n​g​M​e​d​i​a​W​i​t​h​I​n​f​o​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​i​n​f​o​
{​
 ​ ​ ​ ​U​I​I​m​a​g​e​ ​*​i​m​a​g​e​ ​=​ ​i​n​f​o​[​U​I​I​m​a​g​e​P​i​c​k​e​r​C​o​n​t​r​o​l​l​e​r​O​r​i​g​i​n​a​l​I​m​a​g​e​]​;​
 ​ ​ ​ ​
 ​ ​ ​ ​/​/​ ​S​t​o​r​e​ ​t​h​e​ ​i​m​a​g​e​ ​i​n​ ​t​h​e​ ​B​N​R​I​m​a​g​e​S​t​o​r​e​ ​f​o​r​ ​t​h​i​s​ ​k​e​y​
 ​ ​ ​ ​[​[​B​N​R​I​m​a​g​e​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​s​e​t​I​m​a​g​e​:​i​m​a​g​e​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​K​e​y​:​s​e​l​f​.​i​t​e​m​.​i​t​e​m​K​e​y​]​;​
 ​ ​ ​ ​
 ​ ​ ​ ​i​m​a​g​e​V​i​e​w​.​i​m​a​g​e​ ​=​ ​i​m​a​g​e​;​

 ​ ​ ​ ​[​s​e​l​f​ ​d​i​s​m​i​s​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​A​n​i​m​a​t​e​d​:​Y​E​S​ ​c​o​m​p​l​e​t​i​o​n​:​n​i​l​]​;​
}​

Each time an image is captured, it will be added to the store. Both the BNRImageStore and the BNRItem will know the key for the image, so both will be able to access it as needed.

Similarly, when an item is deleted, you need to delete its image from the image store. At the top of BNRItemStore.m, import the header for the BNRImageStore and add the following code to removeItem:.

#​i​m​p​o​r​t​ ​"​B​N​R​I​m​a​g​e​S​t​o​r​e​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​I​t​e​m​S​t​o​r​e​

-​ ​(​v​o​i​d​)​r​e​m​o​v​e​I​t​e​m​:​(​B​N​R​I​t​e​m​ ​*​)​i​t​e​m​
{​
 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​k​e​y​ ​=​ ​i​t​e​m​.​i​t​e​m​K​e​y​;​

 ​ ​ ​ ​[​[​B​N​R​I​m​a​g​e​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​d​e​l​e​t​e​I​m​a​g​e​F​o​r​K​e​y​:​k​e​y​]​;​

 ​ ​ ​ ​[​s​e​l​f​.​p​r​i​v​a​t​e​I​t​e​m​s​ ​r​e​m​o​v​e​O​b​j​e​c​t​I​d​e​n​t​i​c​a​l​T​o​:​i​t​e​m​]​;​
}​

You might be thinking, Why not give the BNRItem a pointer to the image? After all, isn’t a pointer to the image a more direct way of referring to the image? While this is correct, you must consider what happens when you begin saving the items and their images to the filesystem in Chapter 18.

When a UIImage is first created, it exists in memory at a specific address. A pointer holds onto this address so you can refer to the image again. The next time the application launches, however, the image will not be at the same address in memory, so you cannot use the same pointer to access it. Instead, the key will be used to name the image file on the filesystem and each BNRItem will hang on to its key. When you want to load the image back into memory, the BNRImageStore will use the itemKey of a BNRItem to find the image file on the filesystem, load it into memory, and return a pointer to the new UIImage instance. Therefore, the key is a persistent way of referring to an image.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset