• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Property inheritance
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Property inheritance


  • Subject: Property inheritance
  • From: William Squires <email@hidden>
  • Date: Wed, 04 Jul 2012 09:32:58 -0500

Here's my class graph:

Shape (base class - inherits only from NSObject)
LineShape : Shape
RectangleShape : Shape
SquareShape : RectangleShape

In RectangleShape, I define:

@property (nonatomic, assign) CGSize rectSize;
@property (nonatomic, readonly) CGFloat area;
@property (nonatomic, readonly) CGFloat perimeter;

and in RectangleShape.m, I implement these manually, except for rectSize, which I @synthesize:

...
#pragma mark - Non-synthesized accessors

-(CGFloat)area
{
return (self.rectSize.width * self.rectSize.height);
}

-(CGFloat)perimeter
{
return (self.rectSize.width * 2.0 + self.rectSize.height * 2.0);
}
// other stuff...
...

I now want SquareShape to also have the 'area' and 'perimeter' properties, but these should be read/write as there's a direct correlation between the area and the side of the square; ditto for the perimeter. If a subclass (SquareShape) redefines the @property in a superclass as readwrite, are there any 'gotcha's I need to watch out for?

i.e.

SquareShape.h
#import "RectangleShape.h"

@interface SquareShape : RectangleShape

@property (nonatomic, readwrite, assign) CGFloat area;
@property (nonatomic, readwrite, assign) CGFloat perimeter;

@end

SquareShape.m
#import "SquareShape.h"

@implementation SquareShape

#pragma mark - Non-synthesized accessors

-(CGFloat)area
{
return (self.rectSize.width * self.rectSize.height);
}

-(void)setArea:(CGFloat)area
{
CGFloat sideLength = sqrt(area);
CGSize squareSize = CGSizeMake(sideLength, sideLength);
self.rectSize = squareSize;
}

-(CGFloat)perimeter
{
return (self.rectSize.width * 2.0 + self.rectSize.height * 2.0);
}

-(void)setPerimeter:(CGFloat)perimeter
{
CGFloat sideLength = perimeter / 4.0;
CGSize squareSize = CGSizeMake(sideLength, sideLength);
self.rectSize = squareSize;
}

@end


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Property inheritance
      • From: Ken Thomases <email@hidden>
  • Prev by Date: Re: Stupid block syntax!
  • Next by Date: Re: Dynamic modification of text search results
  • Previous by thread: Crash in System Prefs when installing PrefPane
  • Next by thread: Re: Property inheritance
  • Index(es):
    • Date
    • Thread