iphone - Why does this code example on Apple's dev site declare three interfaces for the same class? -
While trying to understand purpose-I am diving into iOS development and I am still at that stage where Sometimes I see, I see things that do not make sense to an experienced programmer like me, announces a class interface from header files, three different times ...
< Pre> @ interface session manager: NSBKD & lt; G2CessionDelegate & gt; {NSString * sessionID; Jijunism * iiGijation; NSString * currentConfPeerID; NSMutableArray * peerList; Id lobby deliaget; Id game delete; ConnectionState sessionState; } @ Property (Nonomatic, Readonly) NSString * currentConfPeerID; @property (nontomic, readonly) NSmutableEre * Pirlist; @protecti (nanatomic, assign) id lobby deliaget; @protecti (nanatomic, assign) id game delete; - (zero) setup session; - (Zero) Connect: (NSString *) PIRID; - (BOOL) accepted; - (zero) done two-dimensional exploration; - (Zero) Send Packet: (NSDT *) Data Type: (Packet Type) Type; - (zero) disconnect curtant; - (NSString *) displayNameForPeer: (NSString *) PERID; @And / Exxchange Extensions for Private Agencies @ Interface Session Manager () - (BOOL) Comparison PIR: PIRID (NSString *); - (BOOL) isReadyToStart; - (zero) voice discredited start; - (zero) deleted session; - (zero) willpower: (nsnification *) notification; - (zero) will be resumed: (nsnification *) notification; @ And @ Interface session manager (voicemomaker) & lt; GKYSAT Client & gt; - (zero) setupVoice; @end
I think each interface is different, but specify the same class name.
- What is the reason for this?
- I have also seen this in other code examples, instead of declaring several interfaces in the header file, see an additional @ interface block declared at the top of the MM implementation file, usually @ above the Implementation Block . Why?
Thank you very much for your knowledge!
They are called categories , and you can enter the parentheses after the class name Can see them.
They use them all to a large herd. They can be kept apart from the announcement of the main category. Especially this Useful in M files, where you may need to create utility methods for your class, but you do not want to show them other items for any reason (so you do not put them in the.), Which are imported by other classes ) Another common use is for group methods that have a certain logical category , informal protocol, or what you have. Categories can be named ( @interface MyClass (MyCategory)
) or anonymous ( @interface MyClass ()
). Later, your headers are usually used for generic personal methods.
(You need categories to declare private methods in your M, so the compiler knows about the methods - otherwise, warning you when you try to call such a method .)
In addition, you can use categories to add methods of existing classes. For example, UIKit has a category on NSString called NSString (UIStringDrawing). Or if you want to create your own:
@firstString; NSString (MyFoo) + (NSString *); @ And // ... somewhere else ... @ implementation NSString (MyFoo) + (NSString *) fooString {return @ "Foo!" ;; } @end
Note that you can not add an example variable with a category.
Comments
Post a Comment