user2984668
user2984668

Reputation: 33

Missing a fundamental understanding wrt methods

I am an OLD procedural language programmer, trying to learn Obj C. And after pulling out all my hair realize I am missing a fundamental understanding

When I alloc an instance of an object ( myObj), give it some data via methods, and the call a method/send a message to that instance, can the implementation routine assume that all the data in that instance is available?

i.e [myObj doSomeStuff] can the implementation of doSomeStuff assume that all the instance data of myObj is available or do I need to send in as arguments of all the parts and pieces of myObj that I need?

Upvotes: 3

Views: 68

Answers (2)

Bruce Dean
Bruce Dean

Reputation: 2828

Ditto on what Luis and rmaddy said. Some of this is best illustrated with an example. Lets consider a Circle class object:

Here is the .h file:

//  Circle.h

#import <Foundation/Foundation.h>

@interface Circle : NSObject
{
    double radius;
    double pi;
}

@property double radius, pi;

-(double) area;
-(double) diameter;
-(double) circumference;

@end

And then the implementation file (note that I have defined instance methods as Luis points out):

//  Circle.m

#import "Circle.h"

@implementation Circle

@synthesize radius, pi;

// Initialize with default radius:
- (instancetype)init {
    self = [super init];
    if (self) {
        pi = 3.14159;
        NSLog(@"Circle created.");
    }
    return self;
}

-(double) area {
    return pi*radius*radius;
}

-(double) diameter {
    return 2*radius;
}

-(double) circumference {
    return 2*pi*radius;
}

@end

Now create a circle object in main and send messages to return several relevant quantities:

//  main.m

#import <Foundation/Foundation.h>
#import "Circle.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {

        Circle *aCircle = [[Circle alloc] init];

        [aCircle setRadius:2];

        NSLog(@"Area = %f",[aCircle area]);
        NSLog(@"Circumference = %f",[aCircle circumference]);
        NSLog(@"Diameter = %f",[aCircle diameter]);
        NSLog(@"Check pi = %f",[aCircle pi]);

    }
    return 0;
}

You can see from this example the value of pi is set when the object is created and then is stored as part of the object, available for other calculations.

Note: you wouldn't want to define pi this way in practice, this is just a simple example to illustrate the point.

Note also that I could have set other internal values of the object after it was created, then this data is also available from that point forward, subject to the qualifications pointed out in the post.

Does this make better sense? You should run this code and experiment with the idea until you feel more comfortable with it.

EDIT: At Andrew Madsen's suggestion, updated the accessor methods for area, diameter, and circumference; these should not be prefixed with 'get'

Upvotes: 3

LuisEspinoza
LuisEspinoza

Reputation: 8538

YES, if the method is an instance method (those who starts with a "-" not "+").

Upvotes: 0

Related Questions