Understanding Yourself in Objective-C

Below is the code from the iTunes U course on iPhone dev in Objective-C. I read the Apple documentation, and all this is very clear, except for myself. I kind of understand myself to be a pointer to myself, but what exactly does this mean? In the code below, what exactly does the concept mean? What is the difference between self.topSpeed ​​and self.nearestWormhole in the implementation file, or does it refer to the same thing in both cases? Is self.topSpeed ​​a reference to Planet * and self.nearestWormhole to reference Wormhole *? Thanks to everyone who answers, I learned C and am now trying to learn OOP, so any input is welcome.

(Header file)
#import "Vehicle.h"
#import "Planet.h"
@interface Spaceship : Vehicle
@property (nonatomic) double topSpeed;
- (void)orbitPlanet:(Planet *)aPlanet
         atAltitude:(double)km;
@end





(Implementation file)
#import "Spaceship.h"
@interface Spaceship()
@property (nonatomic, strong) Wormhole *nearestWormhole;
@end

@implementation Spaceship
@synthesize topSpeed = _topSpeed;
@synthesize nearestWormhole = _nearestWormhole;

- (void)setTopSpeed:(double)speed
{
    if ((speed < 1) && (speed > 0)) _topSpeed = speed;
}

- (void)orbitPlanet:(Planet *)aPlanet atAltitude:(double)km
{
    double speed = self.topSpeed;
    if (speed > MAX_RELATIVE) speed = MAX_RELATIVE;
    [self.nearestWormhole travelToPlanet:aPlanet
                                 atSpeed:speed];
}
@end
+5
source share
5 answers

self ( this ++) , ( " " ).

, , , , . ,

- .

- .

.

:

1. .

. "" , . , , .

2. , .

, , - . : " , ?" .

, , , . (, ), , . , "" "foo", , foo - , - ... , , , , - , foo , Skip foo - ... "foo"? ! foo: self. "", ​​, , .

3. .

, , . .

self , . , , ( , , ), (, myWingman.topSpeed) - self.

+4

, . , C, . , struct, , - . - :

typedef struct { int numerator; int denominator; } Fraction;

Fraction *newFraction(int numer, int denom)
{
   Fraction *result = (Fraction *)malloc(sizeof(Fraction)); // allocate
   result->numerator = numer;
   result->denominator = denom;
   return result;
}

Fraction *multiplyFraction(Fraction *left, Fraction *right)
{

   Fraction *result = (Fraction *)malloc(sizeof(Fraction)); // allocate
   result->numerator = left->numerator * right->numerator;  // multiple (ignoring reduction)
   result->denominator = left->denominator * right->denominator;
   return result;
}

:

Fraction *half = newFraction(1, 2);
Fraction *twothirds = newFraction(2, 3);

Fraction *onethird = multiplyFraction(half, twothirds); // results is 2/6 as we don't reduce in this example

ADT - - . , ( "" ) , , .

- , . " multiplyFraction, " " multiplyFraction ". Objective-C, :

Fraction *onethird = multiplyFraction(half, twothirds);

:

Fraction *onethird = [half multiplyFraction:twothirds];

" " " " - Objective-C multipleFraction, half, twoThirds.

! Objective-C multiplyFraction:

- (Fraction *) multiplyFraction:(Fraction *)right
{

   Fraction *result = [Fraction new]; // allocate
   result->numerator = ????->numerator * right->numerator;
   result->denominator = ????->denominator * right->denominator;
   return result;
}

????. , (right), ( left). Objective-C , - "" ( "ADT" ), . , , self:

- (Fraction *) multiplyFraction:(Fraction *)right
{

   Fraction *result = [Fraction new]; // allocate
   result->numerator = self->numerator * right->numerator;
   result->denominator = self->denominator * right->denominator;
   return result;
}

- self - .

- , :

  • "" - "" struct;
  • - , @interface... struct...; , () (struct) , (`@interface);
  • ( ADT );
  • .

Objective-C C struct...

+2

C . , , @synthesize -.

,

self.topSpeed

topSpeed. "self", (ivars) ( ).

. , topSpeed ​​ "".

self , :

  • INIT
  • dealloc

, .

0

self , . self Spaceship.

self ( ), singleton, . singleton, [Spaceship class]. self, , , factory, .

, , . :

self.topSpeed ​​ Planet * self.nearestWormhole, *?

Wormhole *nearestWormhole Wormhole nearestWormhole. , self.nearestWormhole, Workhole. Spaceship _nearestWormhole self.nearestWormhole . - spaceship.nearestWormhole, .

0

'self' , .. Spaceship. "" , self .

-1

All Articles