16

How to Obtain Class Objects to Automatically Display Behavior

 3 years ago
source link: https://www.codesd.com/item/how-to-obtain-class-objects-to-automatically-display-behavior.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Obtain Class Objects to Automatically Display Behavior

advertisements

I'm having trouble understanding a finer point of Writing and calling Classes. It's probably easlier to grasp in Swift but it bothers me to start that study without getting it right in obj_c first. Currently I do everything in the ViewControllers with iVars and Globals. With two apps 18 months in the App store its overdue to put them right.

I've formed a notion that properties are the Object's State, and any methods within determine the Objects Behaviour but so far no-one is able to tell me.

here be a typical Class header:

@interface Math : NSObject

@property (nonatomic, assign) int a;
@property (nonatomic, assign) int b;
@property (nonatomic, assign) int c;

-(int)mathemagic:(int)a adding:(int)b;

@end

and the corresponding Class implementation:

@implementation Math

@synthesize a = _a;
@synthesize b = _b;
@synthesize c = _c;

- (instancetype)init {
    self = [super init];
    if (self) {
       _a = 0;
       _b = 0;
       _c = 0;
    }
    return self;
}

-(int)mathemagic:(int)a adding:(int)b {
    _c = (a + b);
    return _c;
}

@end

and finally in the appropriate places in my ViewController

#import "Math"

- (void)viewDidLoad {

    [super viewDidLoad];

    Math *theMath = [Math alloc]; // makes no difference if I init[]

    theMath.a = 10;
    theMath.b = 20;
    NSLog (@" answer is %i",theMath.c);
    // but still outputs to:
    // answer is 0
 }

Now I know can make an iVar and do it this way,

int d = [self.theMath mathemagic:theMath.a adding:theMath.b];
NSLog (@" sum: %i",d);

But i shouldn't have to. Stanford CS193P seems to always make the Class a property of the ViewController, but then everything is again expressed as self.theMath.whatever and the Data Model is no longer encapsulated away from the VC ? Maybe Stanford leaves advanced distractions to Java graduates till later.

Well for this person who's read David Flanagan's "Java in A Nutshell" , and Niemeyer-Knudsen's "Learning Java", It's later Now.

I shouldn't have to touch theMath.c, just by assigning values to [ theMath.a ] and [ theMath.b ] should be enough.

Where am I wrong?


I think that is because you are setting a and b = 0 in alloc init . and you are not calling [self mathemagic:a adding:b] anywhere.

I think im Math.m you should change -(instancetype)init to

    - (instancetype)initWith:(int)a andb:(int)b {
self = [super init];
if (self) {
    _c = [self mathemagic:a adding:b];
}
return self;

and in viewDidLoad use

     Math *theMath = [[Math alloc]initWith:10 andb:20];

Hope this helps :)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK