Saturday, 19 May 2012

sample code of category in objective-c



Appdelegate.h:

//
//  CategoriesExampleAppDelegate.h
//  CategoriesExample
//
//  Created by santoshkumar on 27/11/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import <UIKit/UIKit.h>

@class CategoriesExampleViewController;

@interface CategoriesExampleAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    CategoriesExampleViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet CategoriesExampleViewController *viewController;

@end

.m:

//
//  CategoriesExampleAppDelegate.m
//  CategoriesExample
//
//  Created by santoshkumar on 27/11/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "CategoriesExampleAppDelegate.h"
#import "CategoriesExampleViewController.h"
#import "MyString.h"

@implementation CategoriesExampleAppDelegate

@synthesize window;
@synthesize viewController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
   
    NSString *str = [[NSString alloc]init];
    NSLog(@"result is %@",[str defaultString]);
  
    // Override point for customization after app launch   
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
   
    return YES;
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


@end

controllercategory.h:

//
//  MyString.h
//  CategoriesExample
//
//  Created by santoshkumar on 27/11/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSString(MyOwnMethods)
-(NSString*)defaultString;
@end


.m:                                  

//
//  MyString.m
//  CategoriesExample
//
//  Created by santoshkumar on 27/11/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MyString.h"


@implementation NSString(MyOwnMethods)

-(NSString*)defaultString{

    return @"My Own String";
}
@end




0 comments:

Post a Comment