13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 2: Object-Oriented Programming 35Methods• @private: The following variables are only accessible within the definingclass. Subclasses do not inherit access.• @package: A special case, instance variables are accessible as though in@public scope from within the same executable image (such as a frameworkor plug-in), but are considered @private from without. This is mostly used byApple for tightly-coupled framework classes where instance variable accessis shared but should not be exported outside that framework.Method declarations in <strong>Objective</strong>-C are quite different from those you already know. For onething, the only items that are parenthesized are types (the return type and the types of eachargument). The arguments themselves are also interspersed within the method name in amanner similar to when passing a message (see Figure 2-3). You can see the structure of an<strong>Objective</strong>-C method declaration in Figure 2-3.Figure 2-3. Breaking down an <strong>Objective</strong>-C method declarationOne thing to note is that the colon characters that precede each argument are part of themethod name. In Figure 2-3, the method name recorded by the compiler (and used by theruntime to call it) is add:to:, not addto. You might also note that, unlike languages such asC++ that provide function overloading, the method name does not make note of the return typeor argument types. This means that methods can be referred to in code in the most obvious anddirect manner; contrast that with the intricately mangled method names generated by theC++ compiler.The one item not explicitly called out in Figure 2-3 is the hyphen at the start of the method. Thisis the same hyphen you encountered earlier when learning about allocation and initialization.When defining a method, you start the line with a hyphen to denote that this is an instancemethod, and you use a plus to denote a class method. The layout is otherwise the same as thatused in message passing; the only other additions are the parenthesized return and argumenttypes placed before the method and argument names, as in Listing 2-8.Listing 2-8. A Class with Both Class and Instance Methods@interface MyObject : NSObject{int var1;}www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!