Fast Enumeration

The newest version of Objective-C added a few syntax changes to the language. One if these is fast enumeration. Before Objective-C 2.0, iterating through an NSArray looked like this:

f​o​r​ ​(​i​n​t​ ​i​ ​=​ ​0​;​ ​i​ ​<​ ​[​i​t​e​m​s​ ​c​o​u​n​t​]​;​ ​i​+​+​)​ ​{​
 ​ ​ ​ ​P​o​s​s​e​s​s​i​o​n​ ​*​i​t​e​m​ ​=​ ​[​i​t​e​m​s​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​i​]​;​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​i​t​e​m​)​;​
}​

Now you can write that code segment much more succinctly with fast enumeration in main.m.

 ​ ​ ​ ​f​o​r​ ​(​P​o​s​s​e​s​s​i​o​n​ ​*​i​t​e​m​ ​i​n​ ​i​t​e​m​s​)​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​i​t​e​m​)​;​

 ​ ​ ​ ​[​i​t​e​m​s​ ​r​e​l​e​a​s​e​]​;​
 ​ ​ ​ ​i​t​e​m​s​ ​=​ ​n​i​l​;​

 ​ ​ ​ ​[​p​o​o​l​ ​d​r​a​i​n​]​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Now that we have covered the basics of Objective-C, the next chapter will discuss memory management in Cocoa Touch.

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

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