目录
- 1 Protocols and Categories
- Item 23:Use Delegate and Data Source Protocols for Interobject Communication
- Item 24: Use Categories to Break Class Implementations into Manageable Segments
- Item 25: Always Prefix Category Names on Third-Party Classes
- Item 26: Avoid Properties in Categories
- Item 27: User the Class-Continuation Category to Hide Implementation Detail
- Item 28: Use a Protocol to Provide Anonymous Objects
- 2 Reference
1 Protocols and Categories
Item 23:Use Delegate and Data Source Protocols for Interobject Communication
- Use
Data Source
to provide data toclient object
; - Use
Delegate
to respond toclient object
’s event; - Including
client object
parameter in protocol methods so that oneData Source
orDelegate
can responds to multipleclient object
1
2
3
4
5
6
7
8
-(void)networkFecher:(EOCNetworkFecther *)fecher
didReceiveData:(NSData*)data{
if(fetcher == self.myFetcherA){
//Handle data
}else{
//Handle data
}
}
Item 24: Use Categories to Break Class Implementations into Manageable Segments
-
Use categories to split a class implementation into more manageable fragments.
-
Create a category called
Private
to hide implementation detail of methods that should be considered as private.
Item 25: Always Prefix Category Names on Third-Party Classes
-
Always prepend your naming prefix to the names of categories you add to classes that are not your own.
-
Always prepend your naming prefix to the method names within categories you add to classes that are not your own. However prepend makes code autocompletion difficult since what in your mind is the method name after prefix. The suggestied solution was to use suffix
method_ABC
insted of prefixABC_method
Nowadays the Xcode autocompletion is more advanced to return matching even the keyword you typed in the middle of the method. As a result, stick to prepend your naming prefix to the method names within categories.
1
2
3
@interface NSString(ABC_HTTP)
-(NSString n*)ABC_urlEncodedString;
Item 26: Avoid Properties in Categories
-
Keep all property declarations for encapsulated data in the main interface definition.
-
Prefer accessor methods to property declarations in categories, unless it is a class-continuation category.
Item 27: User the Class-Continuation Category to Hide Implementation Detail
-
Use the class-continuation category to add private properties, private methods, private protocols;
-
Redeclare properties in the class-continuation category as read-write if they are read-only in the main inteface, if the setter accessor is requred internally within the class.
Item 28: Use a Protocol to Provide Anonymous Objects
-
Protocols can be used to provide some level of anonymity to types. The type can be reduced to an
id
type that implements a protocol’s methods. -
Use anonymous objects when the type (class name) should be hidden.
-
Use anonymous objects when the type is irrelevant, and the fact that the object responds to certain methods (the ones defined in the protocol) is more important.