NSDictionary/NSMutableDictionary

As you know, arrays are indexed by a number; it is easy to ask Give me object 10. Dictionaries are indexed by a string; it is easy to ask Give me the object stored under the key favoriteColor. More precisely, a dictionary is a collection of key-value pairs. The key is typically a string, and the value can be any sort of object. These key-value pairs are not kept in any particular order.

Let’s make a dictionary of executives. The key will be an executive title, and the value will be an instance of Employee. The first employee will be put in the dictionary under @"CEO"; the second under @"CTO". Change your main.m to create and populate the NSMutableDictionary. Then, print out the dictionary of executives. Finally, set the pointer to the dictionary to nil so that you can see the dictionary being deallocated.

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​ ​a​r​r​a​y​ ​o​f​ ​E​m​p​l​o​y​e​e​ ​o​b​j​e​c​t​s​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​e​m​p​l​o​y​e​e​s​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​ ​d​i​c​t​i​o​n​a​r​y​ ​o​f​ ​e​x​e​c​u​t​i​v​e​s​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​*​e​x​e​c​u​t​i​v​e​s​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​f​o​r​ ​(​i​n​t​ ​i​ ​=​ ​0​;​ ​i​ ​<​ ​1​0​;​ ​i​+​+​)​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​ ​i​n​s​t​a​n​c​e​ ​o​f​ ​E​m​p​l​o​y​e​e​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​E​m​p​l​o​y​e​e​ ​*​p​e​r​s​o​n​ ​=​ ​[​[​E​m​p​l​o​y​e​e​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​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​t​e​r​e​s​t​i​n​g​ ​v​a​l​u​e​s​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​p​e​r​s​o​n​ ​s​e​t​W​e​i​g​h​t​I​n​K​i​l​o​s​:​9​0​ ​+​ ​i​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​p​e​r​s​o​n​ ​s​e​t​H​e​i​g​h​t​I​n​M​e​t​e​r​s​:​1​.​8​ ​-​ ​i​/​1​0​.​0​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​p​e​r​s​o​n​ ​s​e​t​E​m​p​l​o​y​e​e​I​D​:​i​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​P​u​t​ ​t​h​e​ ​e​m​p​l​o​y​e​e​ ​i​n​ ​t​h​e​ ​e​m​p​l​o​y​e​e​s​ ​a​r​r​a​y​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​e​m​p​l​o​y​e​e​s​ ​a​d​d​O​b​j​e​c​t​:​p​e​r​s​o​n​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​I​s​ ​t​h​i​s​ ​t​h​e​ ​f​i​r​s​t​ ​e​m​p​l​o​y​e​e​?​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​i​f​ ​(​i​ ​=​=​ ​0​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​e​x​e​c​u​t​i​v​e​s​ ​s​e​t​O​b​j​e​c​t​:​p​e​r​s​o​n​ ​f​o​r​K​e​y​:​@​"​C​E​O​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​}​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​I​s​ ​t​h​i​s​ ​t​h​e​ ​s​e​c​o​n​d​ ​e​m​p​l​o​y​e​e​?​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​i​f​ ​(​i​ ​=​=​ ​1​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​e​x​e​c​u​t​i​v​e​s​ ​s​e​t​O​b​j​e​c​t​:​p​e​r​s​o​n​ ​f​o​r​K​e​y​:​@​"​C​T​O​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​}​

 ​ ​ ​ ​ ​ ​ ​ ​}​

 ​ ​ ​ ​ ​ ​ ​ ​…​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​a​l​l​A​s​s​e​t​s​:​ ​%​@​"​,​ ​a​l​l​A​s​s​e​t​s​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​e​x​e​c​u​t​i​v​e​s​:​ ​%​@​"​,​ ​e​x​e​c​u​t​i​v​e​s​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​e​x​e​c​u​t​i​v​e​s​ ​=​ ​n​i​l​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​P​r​e​d​i​c​a​t​e​ ​*​p​r​e​d​i​c​a​t​e​ ​=​ ​[​N​S​P​r​e​d​i​c​a​t​e​ ​p​r​e​d​i​c​a​t​e​W​i​t​h​F​o​r​m​a​t​:​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​@​"​h​o​l​d​e​r​.​v​a​l​u​e​O​f​A​s​s​e​t​s​ ​>​ ​7​0​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​t​o​B​e​R​e​c​l​a​i​m​e​d​ ​=​ ​[​a​l​l​A​s​s​e​t​s​ ​f​i​l​t​e​r​e​d​A​r​r​a​y​U​s​i​n​g​P​r​e​d​i​c​a​t​e​:​p​r​e​d​i​c​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​t​o​B​e​R​e​c​l​a​i​m​e​d​:​ ​%​@​"​,​ ​t​o​B​e​R​e​c​l​a​i​m​e​d​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​t​o​B​e​R​e​c​l​a​i​m​e​d​ ​=​ ​n​i​l​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​G​i​v​i​n​g​ ​u​p​ ​o​w​n​e​r​s​h​i​p​ ​o​f​ ​a​r​r​a​y​s​"​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​a​l​l​A​s​s​e​t​s​ ​=​ ​n​i​l​;​
 ​ ​ ​ ​ ​ ​ ​ ​e​m​p​l​o​y​e​e​s​ ​=​ ​n​i​l​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run the program. The executives dictionary should log itself out:

e​x​e​c​u​t​i​v​e​s​ ​=​ ​{​
 ​ ​ ​ ​C​E​O​ ​=​ ​"​<​E​m​p​l​o​y​e​e​ ​0​:​ ​$​0​ ​i​n​ ​a​s​s​e​t​s​>​"​;​
 ​ ​ ​ ​C​T​O​ ​=​ ​"​<​E​m​p​l​o​y​e​e​ ​1​:​ ​$​1​5​3​ ​i​n​ ​a​s​s​e​t​s​>​"​;​
}​

Figure 21.3  Two instances of Employee in an NSMutableDictionary

Two instances of Employee in an NSMutableDictionary
..................Content has been hidden....................

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