HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Working with Classes (inheriting), @ properties and Initialization

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withpropertiesworkinginitializationclassesandinheriting

Problem

I'm working on this Objective C programming assignment I found online. I'm not sure if I have met all the requirements, especially part C. Any help or suggestion will be appreciated.


Part 6


a) Implement class A with properties a1, a2, and a3 (int,
string, int).


b) New objects are automatically initialized to 1, "hello", 1.


c) Also provide initializer to any data and constructor (called
without alloc) to do the same.


d) Make sure %@ ob object of A will print all data.


e) Then implement B inheriting from A. B adds property b (string).


f) Make sure B works as A, that is new object is initialized to 1, "hello", 1, and 3 (the new data). The rest also must work on B.

```
//classA.h file
#import

@interface ClassA : NSObject
// Part 6a
@property int a1;
@property NSString *a2;
@property int a3;

-(NSString *) description;
-(id) initWithA1: (int) x andA2: (NSString *) s andA3: (int) y;
-(id) init;
@end

//classA.m file
#import "ClassA.h"

@implementation ClassA

-(id) initWithA1:(int)x andA2:(NSString *)s andA3:(int)y {
self = [super init];
if (self) {
self.a1 = x;
self.a2 = s;
self.a3 = y;
}
return self;
}

// part 6b
  • (id) init {


return [self initWithA1:1 andA2:@"hello" andA3:1];
}

// part 6d
-(NSString *) description {
return [NSString stringWithFormat:@"ClassA a1 = %d , a2 = %@ , a3 = %d", self.a1, self.a2, self.a3];
}

@end

//classB.h file
#import "ClassA.h"

@interface ClassB : ClassA
@property int a1;
@property NSString *a2;
@property int a3;
@property NSString * b;

-(NSString *) description;
-(id) initWithA1:(int)x andA2:(NSString )s andA3:(int)y andB: (NSString ) z;
-(id) init;
@end

//classB.m file
#import "ClassB.h"

@implementation ClassB

-(id) initWithA1:(int)x andA2:(NSString )s andA3:(int)y andB:(NSString )z {
self = [super init];
if (self) {
self.a1 = x;
self.a2 = s;
self.a3 =

Solution

This is my first review so bear that in mind when you are reading it.

The first thing I see is that in the initialization, you should not use the automatic setters and getters for properties. The reason for this is that the object may not be completely initialized, and thus may fail. So instead of doing:

self.a1 = x;
self.a2 = s;
self.a3 = y;


access the properties directly like so:

_a1 = x; 
_a2 = s;
_a3 = y;


Otherwise, your init pattern is good, and bonus points for having a default -(id)init that calls your designated initializer.

For the names of all of these, I would make sure to always use a more descriptive name. It is probably okay in such a simple example to use a1, x, y, etc, but for anything more complicated, use names that describe in more detail exactly what the variable is.

In the description method it looks like you are calling the int values with %d. I can't think of a reason not to use %i, but maybe I am wrong here.

One last thing, in the interface the spacing inside the declaration of -(id)initWithEtc is loose. It looks correct in the implementation though.

Code Snippets

self.a1 = x;
self.a2 = s;
self.a3 = y;
_a1 = x; 
_a2 = s;
_a3 = y;

Context

StackExchange Code Review Q#57834, answer score: 5

Revisions (0)

No revisions yet.