What is the actual use of categories instead of inheritance?

I am trying to understand the actual use of categories as opposed to inheritance in Objective-C. When do I prefer to use a category? A real code example will be helpful.

0
source share
6 answers

I used categories when I need to add some convenient functions that I will reuse for an existing class, without the need for a subclass to overwrite some existing functions of this class.

For example, when I want to check an empty string or remove all the leading and trailing spaces of a string:

.h file:

@interface NSString  (Extension)

-(BOOL)isEmptyString;
-(NSString *)trimLeadingAndTrailingWhiteSpaces;

@end

.m file:

@implementation NSString  (Extension)

-(BOOL)isEmptyString
{
    NSString *myString = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (myString.length == 0)
        return TRUE;
    else
       return FALSE;
}

-(NSString *)trimLeadingAndTrailingWhiteSpaces
{
    NSString *myString = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return myString;
}
@end

To use it:

 someString = [someString trimLeadingAndTrailingWhiteSpaces];

 if ([someString isEmptyString])
       {
         //someString is empty, do whatever!
       }
+2
source

.

, NSDate NSString, . , .

API:

1/NSDictionary , .
2/ NSString UI, . : , . , .

+3

, . , . . . , , . c, , , .

, , , , NString . - NSString

NSString-Encryption.h

   @interface NSString (Encryption)
     -(NSString*) encrypt;
    @end

NSString-Encryption.m

#import "NSString-Encryption.h"
@implementation NSString (Encryption)
 -(NSString*) encrypt
{ 
 // your encryption method here 
  return encryptedString; 
}
@end

UseNSString-encryption.m

 NSString *testString = @"this is test"; 
 NSString *encryptedString = [testString encrypt];

, , . NSMutableString , NSString. .

, .

, , c . .m .h.

.h

@interface Phone:NSObject
 -(void) call; 
 @end 

.m

   @interface Phone(Private)
       -(void) validatePhoneEntry:(NSString*) phoneNumber; 
     @end 

     @implementation Phone 

     -(void) validatePhoneEntry:(NSString*) phoneNumber
     {

     } 

     -(void) call
     {
     }

     @end  

, , - .

+1

. NSString . , , .

0

, . , .

/sorta , , . : 1) ; 2) ; 3) "" , , , undefined. , , , , . , .

, NSData, +(NSData*)dataWithCryptographicallyRandomBytes:(NSUInteger)length -(void)base64Decode. , -writeToFile:atomically:, NSData . -, - , , [super writeToFile:file atmoically:YES]. , , - NSData , .

0

Categories are similar to Java interfaces (in java, implementation is optional) - a way to group methods and make objects meet a specific API - regardless of the type of class. Inheritance makes sense if you need to add additional ivars, and the new class corresponds to the relationship isa (student isa person). Adding several utility methods does not make the isa case - this is usually done with protocols. Categories are often used with delegates, where the methods in question are optional.

0
source

All Articles