1
votes

i want to know if the following situation can be done. I have inherited a project of iOS 8. I'd like to add a method to NSObject so that all objects can see it. and I have done this already. Here is the category implementation i have created:

#import "NSObject+myCategory.h"

@implementation NSObject (myCategory)


-(void)localizeStringIncludeDefault{

    NSLog(@"about to localize String");
}

@end

Now i go to a MyViewController.m for example and try to call the method but its not working its not seen:

heres the .h:

    #import <UIKit/UIKit.h>


    @interface MyViewController : UIViewController {
        BOOL someFakeBoolean;
        IBOutlet UIView *someView;
        //etc
    }

    @property (nonatomic,assign) IBOutlet MyViewController *myViewController;
-(void)localizeStringIncludeDefault;

and here is the implementation *.m and my issue:

@implementation MyViewController

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self localizeStringIncludeDefault] //this call cant be done, the method is not visible
}

- (void) viewDidDisappear:(BOOL)animated
{
   //etc 
}

//etc

I mean it makes sense to me that i'd have to import the "NSObject+myCategory.h" into the MyViewController header to use the category but because i've inherited this code it already has a base. I dont want to go into every .h file and import this. Is a easy way to extend object so that EVERY object class sees my method ?

2
Not sure if @property (nonatomic,assign) IBOutlet MyViewController *myViewController; is necessaryNate Lee

2 Answers

4
votes

One option would be to add the category .h file to the pch file. Then it will be seen by every class in your project without the need to import it explicitly.

1
votes

Declare your global variables or declarations in your pch file or rather make a Global.h and just import this in your pch file (helps a lot in reusability). You can declare extern items as well in your Global.h and populate in App Delegate