Reputation: 444
I am a C/Perl/Unix guy learning Objective C. I have a simple OS X app with two classes that I have created. How do I invoke a method from one class from a method in the other class??
Up until now, I wrote simple "proof of concept" apps, creating instance methods within a single class, and a simple main.m:
MyClass.m
#import "MyClass.h"
@implementation MyClass
- (NSString *) methodName {
...some code here...
}
main.m
#import "MyClass.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
MyClass *myClass = [[MyClass alloc] init];
NSString *page;
page = [myClass methodName];
I initialize the class from main.m, which acts as a simple test bench. This is fine for learning how to write simple classes and methods.
Now I've created TWO classes, the second is a collection of string methods that I want to use for parsing within the first class. These methods return substrings, or TRUE/FALSE if substrings exist within a given string.
How do I invoke these substring methods from within another class? Do I need to use class methods? Do I need to move the initialization from main.m to each class to allow the classes to be seen?
As I mentioned, I am a C programmer, but instance/class methods and the scoping of classes is still new to me... Any suggestions, or pointers to documentation (Apple or Third party) would be welcome.
Upvotes: 0
Views: 160
Reputation: 20187
Assuming your new class is called StringUtility, you can do the following:
Declare the methods in your StringUtility class as 'class methods'. This means that you message the class itself, rather than a specific instance of the class. To do this, simply define those methods with a +
rather than a -
at the start of the method declaration in both StringUtility.h
and StringUtility.m
. You then call these methods like this:
+ (NSString *) awesomeTransformationForString:(NSString *)aString
{
NSString * newString = ... // code for your transformation that does something to aString here.
return newString
}
Then, to actually use the class method:
NSString * myString = @"My string for awesomeifying.";
NSString * transformedString = [StringUtility awesomeTransformationForString:mystring];
If the methods in your StringUtility class need to use information they store themselves between times they are called, in order to determine how to act on the objects you pass them, then they need to be 'instance methods'. That means you create an instance of that class and communicate with that:
- (NSString *) awesomeTransformationForString:(NSString *)aString
{
NSString * newString = ... // code for your transformation that does something to aString here.
return newString
}
Then to actually use the instance method:
NSString * myString = @"My string for awesomeifying.";
StringUtility * myStringUtility = [StringUtility new];
NSString * transformedString = [myStringUtility awesomeTransformationForString:mystring];
Note that there is nothing to stop your class from containing both class methods and instance methods. You simply message the class for class methods, and your instance for instance methods. At times you may wish to message a class method of your an object's own class from within an instance. Just simply use [StingUtility methodName]
as you would from anywhere else.
Upvotes: 0
Reputation: 17208
In object-oriented programming a class combines data and code. It's like a struct that also has methods.
If a method doesn't depend on the state of the object, it can be a class method instead. In that case you're just using the class as a sort of namespace where the method can live. If all the methods are class methods, you never need to instantiate the class. (i.e. you don't need [[StringHelpers alloc] init]
).
So… does your second class have data?
If not, just make the methods class methods. Then you can do this:
#import <StringHelpers.h>
[StringHelpers hasSubstring:myOtherString];
Upvotes: 1