Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing a fundamental understanding wrt methods

Tags:

objective-c

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?

like image 736
user2984668 Avatar asked Nov 19 '25 20:11

user2984668


1 Answers

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'

like image 110
Bruce Dean Avatar answered Nov 21 '25 10:11

Bruce Dean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!