jamesHoward
jamesHoward

Reputation: 107

How to call upon a method in objective c?

I am new to objective c and I was just wondering how I would call upon a method that has a return statement value?

Example

-(NSString*) exampleString
{

NSString *example= @"hello this a test";

return example;


}

Now how would I call upon the above method to get the value returned?

Thanks

Upvotes: 0

Views: 3161

Answers (6)

richerd
richerd

Reputation: 1247

Welcome to Objective-C, it's simple. When you are calling an Objective-C method you use [object message] syntax

NSString *exampleString = [self exampleString]; // self refers to the object or class

If you want to pass a parameter this may look like

[self getText:@"hello"];

- (NSString *)getText:(NSString *)text 
{
    return text;
}

Upvotes: 2

El Developer
El Developer

Reputation: 3346

Sir, you have a mistake on your code, it should look like this:

-(NSString *) exampleString{
    NSString *example= @"hello this a test";

    return example;
}

The mistake is that you cannot return a pointer using a method with a return type that is not a pointer (you originally had -(NSString )exampleString ...).

Now to answer your question, this is an instance method, that's because at the beginning of the declaration it uses a dash (-). There are other types of methods like class methods, which are easily differentiated by using a plus sign in the declaration (+).

Since this is an instance method, you need to have an instance to be able to call this method so first you have to declare an instance of the class and then call it, be sure to initialize it and allocate some space (if needed):

JamesHowardClass *myInstance=[[JamesHowardClass alloc] init];
NSLog(@"Rocking out my first method %@", [myInstance exampleString]);

//Since you allocated some space, you should take care of the memory management
[myInstance release];

Now looking at another possible use, if you are calling this method within your class declaration, you should call it like this:

NSLog(@"Rocking out my first method %@", [self exampleString]);

The word self, represents an instance of the class itself within the class, and it can only be used within instance methods, not with class methods.

Upvotes: 1

Tommy
Tommy

Reputation: 100652

You call a method by passing a message to an instance of the class that implements the method. That then evaluates to an rvalue, just like a C function call.

So, e.g.

NSString *fetchedExample = [self exampleString];

EDIT: David's answer below is also worth repeating. In Objective-C objects are opaque and (with a couple of exceptions) live exclusively on the heap, so you pass around pointers to them rather than the things themselves.

Upvotes: 4

Liftoff
Liftoff

Reputation: 25412

NSString *returnedText = [self methodName:parameter];

So in your case,

NSString *returnedText = [self exampleString];

Just a note: make sure you change the return type of the method to "NSString*" instead of "NSString" as NSString objects cannot be declared statically.

Hope this helps

Upvotes: 1

0x90
0x90

Reputation: 6279

NSString *returnedValue = [self exampleString];

Upvotes: 1

Bahamut
Bahamut

Reputation: 1939

NSString *string = [objectContainingTheMethod exampleString];

Check out guides for Objective C programming especially the declaration and usage of methods.

Upvotes: 3

Related Questions