0

I'm working on this assignment I found online (Intermediate App Development Using iOS). I'm stuck on part c and d, don't know exactly what its asking me to do. I know how to print int (%i) and object (%@), but %@ print all data? 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 %@ object of A will print all data.

Here is what I have done so far:

// classA.h
#import <Foundation/Foundation.h>

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

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

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

@implementation ClassA

-(ClassA *) 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
-(ClassA *) init {
    if (self = [super init]) {
        self.a1 = 0;
        self.a2 =@"hello";
        self.a3 = 0;
    }
    return self;
}
@end
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • 1
    You say "part c, part d" etc, but you don't provide a link to the assignment. – trojanfoe Jul 23 '14 at 14:50
  • Its included in the description. I made it more visible with use of white space. – Christopher Palmer Jul 23 '14 at 14:52
  • I still don't see it?!? – trojanfoe Jul 23 '14 at 14:53
  • 1
    Checkout examples how to override `-description` (that's the `d` part) – Sulthan Jul 23 '14 at 14:54
  • Right below my question, it says Part 6 and labeled a b c d? – Christopher Palmer Jul 23 '14 at 14:54
  • @trojanfoe just googled that course assignment and found http://www.outreach.hawaii.edu/pnm/programs/2011/EVENT-L11701.asp based on the assumption this is it, this is part of a course and would come under homework. Which as you know we don't do. – Popeye Jul 23 '14 at 14:54
  • I didn't even used to do my own homework, let alone others... – trojanfoe Jul 23 '14 at 14:55
  • @trojanfoe haha same. `Christopher Palmer` please provide the link as requested to this assignment so we can determine whether this is a piece of homework or not as we don't do users homework for them because you will not learn anything. – Popeye Jul 23 '14 at 14:56
  • @Sulthan so something like this `-(NSString *)description { return [NSString stringWithFormat:@"%@, %@ %@", self.a1, self.a2, self.a3]; }` – Christopher Palmer Jul 23 '14 at 15:16
  • @Popeye: Please note that there is no general rule against homework questions. Of course they should follow the guidelines from http://stackoverflow.com/help/on-topic. More about homework questions here: http://meta.stackoverflow.com/questions/255477/please-clarify-the-policy-on-homework-questions. – Martin R Jul 23 '14 at 15:18

2 Answers2

1

In reference to part "b" of your question:

As a general rule, only 1 initializer should be doing the "real" work. This is often referred to as the designated initializer. So, your init method should probably read something like:

- (id) init
{
    return [self initWithA1:1 andA2:@"hello" andA3:1];
}
orbitor
  • 93
  • 4
  • Thank you. So what I did is not wrong, but better way is what you suggested? – Christopher Palmer Jul 23 '14 at 16:04
  • If by saying 'not wrong' we can mean that the code works for you in this situation, then I'll go with that. Designated initializers is a generally accepted design pattern in Obj-C and Apple frameworks. The `init` method is the default (users can always call alloc/init on any object and expect to get something useful) -- it is your job to make sure that your object is _always_ initialized properly. The easiest way to guarantee that is the designated initializer pattern. – orbitor Jul 23 '14 at 16:37
0

As @orbitor wrote, your class should have one designated initialiser.

So, your init method should probably read something like:

- (id) init
{
    return [self initWithA1:1 andA2:@"hello" andA3:1];
}

In order to print all object you should implement custom description method:

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

According to c: Class method new just calls alloc and init methods so you should only make sure that you wrote properly all initialisers.

Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43