Zeroing of weak references

To see weak references in action, let’s add another array to the mix. What if we wanted an array of all assets – even ones that have not been assigned to a particular employee? We could add the assets to an array as they are created. Add a few lines of code to main.m:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​
#​i​m​p​o​r​t​ ​"​E​m​p​l​o​y​e​e​.​h​"​
#​i​m​p​o​r​t​ ​"​A​s​s​e​t​.​h​"​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​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​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​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​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​a​l​l​A​s​s​e​t​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​ ​1​0​ ​a​s​s​e​t​s​
 ​ ​ ​ ​ ​ ​ ​ ​f​o​r​ ​(​i​n​t​ ​i​ ​=​ ​0​;​ ​i​ ​<​ ​1​0​;​ ​i​+​+​)​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​ ​a​s​s​e​t​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​A​s​s​e​t​ ​*​a​s​s​e​t​ ​=​ ​[​[​A​s​s​e​t​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​G​i​v​e​ ​i​t​ ​a​n​ ​i​n​t​e​r​e​s​t​i​n​g​ ​l​a​b​e​l​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​c​u​r​r​e​n​t​L​a​b​e​l​ ​=​ ​[​N​S​S​t​r​i​n​g​ ​s​t​r​i​n​g​W​i​t​h​F​o​r​m​a​t​:​@​"​L​a​p​t​o​p​ ​%​d​"​,​ ​i​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​a​s​s​e​t​ ​s​e​t​L​a​b​e​l​:​c​u​r​r​e​n​t​L​a​b​e​l​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​a​s​s​e​t​ ​s​e​t​R​e​s​a​l​e​V​a​l​u​e​:​i​ ​*​ ​1​7​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​G​e​t​ ​a​ ​r​a​n​d​o​m​ ​n​u​m​b​e​r​ ​b​e​t​w​e​e​n​ ​0​ ​a​n​d​ ​9​ ​i​n​c​l​u​s​i​v​e​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​U​I​n​t​e​g​e​r​ ​r​a​n​d​o​m​I​n​d​e​x​ ​=​ ​r​a​n​d​o​m​(​)​ ​%​ ​[​e​m​p​l​o​y​e​e​s​ ​c​o​u​n​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​F​i​n​d​ ​t​h​a​t​ ​e​m​p​l​o​y​e​e​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​E​m​p​l​o​y​e​e​ ​*​r​a​n​d​o​m​E​m​p​l​o​y​e​e​ ​=​ ​[​e​m​p​l​o​y​e​e​s​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​r​a​n​d​o​m​I​n​d​e​x​]​;​

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

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​a​l​l​A​s​s​e​t​s​ ​a​d​d​O​b​j​e​c​t​:​a​s​s​e​t​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​E​m​p​l​o​y​e​e​s​:​ ​%​@​"​,​ ​e​m​p​l​o​y​e​e​s​)​;​

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

 ​ ​ ​ ​ ​ ​ ​ ​[​e​m​p​l​o​y​e​e​s​ ​r​e​m​o​v​e​O​b​j​e​c​t​A​t​I​n​d​e​x​:​5​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​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​(​@​"​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​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​s​l​e​e​p​(​1​0​0​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Before you build and run your program, think about what you expect your output to look like. You’ll see the contents of the allAssets array – after Employee #5 has been deallocated. What will the status of Employee #5’s assets be at this point? These assets lose one owner (Employee #5), but they are still owned by allAssets, so they won’t be deallocated.

What about holder for the assets previously owned by Employee #5? When the object that a weak reference points to is deallocated, the pointer variable is zeroed, or set to nil. So Employee #5’s assets will not be deallocated, and their holder variables will be automatically set to nil.

Now build and run the program and check your output:

E​m​p​l​o​y​e​e​s​:​ ​(​
 ​ ​ ​ ​"​<​E​m​p​l​o​y​e​e​ ​0​:​ ​$​0​ ​i​n​ ​a​s​s​e​t​s​>​"​,​
.​.​.​
 ​ ​ ​ ​"​<​E​m​p​l​o​y​e​e​ ​9​:​ ​$​1​3​6​ ​i​n​ ​a​s​s​e​t​s​>​"​
)​
G​i​v​i​n​g​ ​u​p​ ​o​w​n​e​r​s​h​i​p​ ​o​f​ ​o​n​e​ ​e​m​p​l​o​y​e​e​
d​e​a​l​l​o​c​a​t​i​n​g​ ​<​E​m​p​l​o​y​e​e​ ​5​:​ ​$​1​3​6​ ​i​n​ ​a​s​s​e​t​s​>​
a​l​l​A​s​s​e​t​s​:​ ​(​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​0​:​ ​$​0​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​3​:​ ​$​6​8​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​1​:​ ​$​1​7​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​6​:​ ​$​1​1​9​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​2​:​ ​$​3​4​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​7​:​ ​$​3​4​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​3​:​ ​$​5​1​ ​u​n​a​s​s​i​g​n​e​d​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​4​:​ ​$​6​8​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​3​:​ ​$​6​8​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​5​:​ ​$​8​5​ ​u​n​a​s​s​i​g​n​e​d​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​6​:​ ​$​1​0​2​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​6​:​ ​$​1​1​9​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​7​:​ ​$​1​1​9​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​2​:​ ​$​1​1​9​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​8​:​ ​$​1​3​6​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​9​:​ ​$​1​3​6​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​9​:​ ​$​1​5​3​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​1​:​ ​$​1​5​3​ ​i​n​ ​a​s​s​e​t​s​>​>​"​
)​
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​
d​e​a​l​l​o​c​i​n​g​ ​<​L​a​p​t​o​p​ ​3​:​ ​$​5​1​ ​u​n​a​s​s​i​g​n​e​d​>​
.​.​.​
d​e​a​l​l​o​c​i​n​g​ ​<​L​a​p​t​o​p​ ​8​:​ ​$​1​3​6​ ​u​n​a​s​s​i​g​n​e​d​>​

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

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