10

How can I define a method that can be called from anywhere, in every viewcontroller class?

I have a method that brings me a json file, and i want it to be reusable, since i have several json calls on my app.

Can you help me?

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
88fsantos
  • 393
  • 1
  • 7
  • 22
  • Singleton http://en.wikipedia.org/wiki/Singleton_pattern – Evan Mulawski Jun 01 '12 at 15:39
  • rooster117 is correct. You are referring to a static method. Please keep in mind all methods are public in Objective-C. You can however "hide" a method or cause a compiler warning by declaring methods in a class extension. Still, they are not really private. – Oh Danny Boy Jun 01 '12 at 15:43

4 Answers4

6

You can add it through a category:

EDIT

Create a new .h .m file pair and in the .h file:

@interface UIViewController(JSON)
-(void) bringJSON;
-(void) fetchData:(NSData*) data;


@ end

Then in the .m file:

@implementation UIViewController(JSON)

-(void) bringJSON {

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

[NSData dataWithContentsOfURL:yourURL];

[self performSelectorOnMainThread:@selector(fetchData:)
withObject:data waitUntilDone:YES];

});

}


-(void) fetchData:(NSData*) data {

//parse - update etc.

}


@end

Where I'm just assuming that you'll be returning an NSArray, you can put any method there and extend all UIViewControllers. The method bringJSON will be available to all UIViewControllers and its subclasses.

Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41
  • How can I return my json, just when it's loaded? :S If i put it inside de "dispatch_get_main_queue" it get's an error. – 88fsantos Jun 01 '12 at 16:17
  • TKS A LOT! You solved my problem! It would be very hard to became a ios developer without this community – 88fsantos Jun 04 '12 at 10:51
  • I'm glad this helped you. Same here, I learnt more from the community than I learnt from books or online tutorials :) – Kaan Dedeoglu Jun 04 '12 at 12:05
  • How is it then called in the .m file that is importing this .h file? can someone show how it is done please? Also, what in the code is added exactly in the .m file above since I see some code that might be specific to the json method – Lion789 Apr 19 '14 at 03:50
4

I believe you are thinking about a static method which would be defined with the "+" symbol.

+ (String) yourFunctionName:(NSInteger)someValue .....

Then you could call it anywhere with the class name first:

[YourClassName yourFunctionName:5];

If you need to have a function that access an object that needs to be instantiated then you will want to do a singleton pattern.

rooster117
  • 5,502
  • 1
  • 21
  • 19
3

Use a + sign before the return type of the method.

For example:

 + (void) Name: (NSString  *) str{

 }
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Jignesh B
  • 490
  • 7
  • 18
2

I plused the first answer as it is a way of creating (essentially) another object with methods that can be called from any file that includes that object.

Remember also that objective-c also is simply just C. You can have .c files included that are simply contain ANSI-C routines that can be called also.

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • I see, My problem with the classes methods is that, my return is allways getting an error, since it can't use instance variables. Is there a way to use class variables? About the singletons, i think i've to read about it. Singletons are different from protocols? I camed from frontend, and i'm in xcode since about a month ago. So it's being a little hard to do more complicated things. Tks a lot for oyur help guys! – 88fsantos Jun 01 '12 at 15:53
  • You could always implement C++ objects as well. This way you could provide protected / private variables with function access only. XCode does have the ability to compile C++ objects into your Objective-C code. I will try to find some links, but here is a post ABOUT the subject (without sample code) on StackOverflow. http://stackoverflow.com/questions/406753/how-well-is-objective-c-supported – trumpetlicks Jun 01 '12 at 16:04