Reputation: 27618
I am trying to create a global function or method in objectiveC. This is so that I can call it from any of my view controllers.
I already checked out this post: How to create global functions in Objective-C
So what I did was in Xcode 6 I created new file and choose a category drop down. that created these two *.h & *.m files for me. I added my method called SaveWeatherDataNow in it
//NSObject+SaveWeatherData.h
#import <Foundation/Foundation.h>
@interface NSObject (SaveWeatherData)
+(void)SaveWeatherDataNow:(NSString *)XML;
@end
//NSObject+SaveWeatherData.m
#import "NSObject+SaveWeatherData.h"
@implementation NSObject (SaveWeatherData)
+(void)SaveWeatherDataNow:(NSString *)XML
{
NSLog(@"SaveWeatherDataNow XML lenght: %ld", [XML length]);
}
@end
Now in my another view controller if I call this method then complier doesn't find it at all. What did I miss?
// v1CitiesAdd.m
#import "NSObject+SaveWeatherData.h"
@implementation v1CitiesAdd
...
...
-(IBAction) doneAction
{
...
[SaveWeatherData SaveWeatherDataNow:XML]; // Complier Error: Use of undeclared identifier SaveWeatherData
Upvotes: 2
Views: 875
Reputation: 125037
[SaveWeatherData SaveWeatherDataNow:XML]; // Complier Error: Use of undeclared identifier SaveWeatherData
The root of the problem here is that you haven't defined a class named SaveWeatherData
. Instead, you've created a category on NSObject
, so that you can call +SaveWeatherDataNow:
on any class that's derived from NSObject
. The straightforward solution is to create a class instead of a category. Change these lines:
@interface NSObject (SaveWeatherData)
@implementation NSObject (SaveWeatherData)
to these:
@interface SaveWeatherData
@implementation SaveWeatherData
and that should allow a call like this:
[SaveWeatherData SaveWeatherDataNow:XML];
to work correctly.
Upvotes: 0
Reputation: 21157
I am writing a different answer, because I think the best answer to this question would be Don't do it!.
To create a category for NSObject
to save data is a very ugly thing to do.
WHY
It would mean that when you import it to a class EVERY class that inherits from NSObject (almost all of them) would suddenly have that extra method.
For example, code like this would work:
NSNumber *myInt = @(3)
NSString *myXmlAsString = [NSString stringWithFormat:@"<rating>%@</rating>",myInt.integerValue];
If you keep using the static method (with +) you could do:
[NSObject SaveWeatherDataNow:myXmlAsString];
or
[NSNumber SaveWeatherDataNow:myXmlAsString];
or even
[UIImage SaveWeatherDataNow:myXmlAsString];
If you change it to an instance method (with -), you could do:
[myXmlAsString SaveWeatherDataNow:myXmlAsString];
or
[myInt SaveWeatherDataNow:myXmlAsString];
or use most of the existing variables in that scope, which would seem very strange to say the least.
WHAT YOU SHOUD DO INSTEAD
I would use one of following 2 options:
Create a class that manages data saving/loading for you. The usage should then look like this:
[DataManager saveWeatherDataNow:myXmlAsString];
If you don't need that much, only that function, you could create a Utility class and add that static method (in case you do not have one already).
[Utility saveWeatherDataNow:myXmlAsString];
PS: by convention, you usually start method names with lowercase.
Upvotes: 2
Reputation: 4594
Change the +
to a -
(in front of (void)
):
-(void)SaveWeatherDataNow:(NSString *)XML;
That way, you can refer to the method within your SaveWeatherData object.
Don't forget to change it in both the header (.h) file as well as the class (.m) file.
You could also call
[NSObject SaveWeatherDataNow:XML];
by leaving the +.
===
When you use a +, it's a class method. It works globally, and doesn't know or care about individual objects.
When you use a -, it's an object method. It works within a specific object.
So, if there is a Brick class,
+(void) smash;
would change/update every brick to be smashed.
-(void) shatter;
would shatter a specific brick you send that message to.
[myBrick1 shatter]; would only shatter the one brick.
[Brick smash]; would change something globally for all bricks.
Upvotes: 2