#import <Foundation/Foundation.h> #import "../../mem.h" @interface NSDate(description) -(NSString*) description; @end @implementation NSData(description) -(NSString*) description { int len = [self length]; char buff[len]; [self getBytes: buff]; NSMutableString* ret = [NSMutableString string]; int i; for (i=0; i < len; i++) [ret appendFormat: @"%i ", buff[i] ]; return ret; } @end int main(void) { POOL; char arr[]= {10, 20, 30}; NSData* data = [NSData dataWithBytes:arr length: sizeof(arr)/sizeof(char)]; NSLog(@"%@", [data description]); _POOL; return 0; }Note, POOL and _POOL are two macros defined in "../../mem.h" as follow: #define POOL NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init] #define _POOL [pool drain]
12 September 2011
The power of Objective-c categories.. add category for NSData object
Categories is a very powerful feature in objective-c.. we will show how to use it to overrides the function "description" of the NSData object in the following example:
Labels:
objective-c
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment