Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is the first program (that is not doing much basically), and I know usually you define classes in separate files. I just wanted to see if I got the syntax right, and the concept of using objects from the first chapter of the book.

#import <Foundation/Foundation.h>

//interface section
@interface Watch: NSObject

-(void) setHours: (int) h;
-(void) setMinutes: (int) m;
-(int) getHours;
-(int) getMinutes;
-(void) printTime;

@end

//implemetation section

@implementation Watch 

{
    int hours;
    int minuts;
}

- (void) setHours: (int) h
{
    hours = h;
}

- (void) setMinutes:(int) m
{
    minuts = m;
}

- (int) getHours
{
    return hours;
}

- (int) getMinutes
{
    return minuts;
}

-(void) printTime
{
    NSLog(@"Time is %i:%i", hours, minuts);
}

@end

//the program section 

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {

        Watch *myWatch = [[Watch alloc]init];

        [myWatch getHours];

        [myWatch getMinutes];

        [myWatch setHours: 5];

        [myWatch setMinutes: 30];

        [myWatch printTime];

    }
    return 0;
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

You should definitely check out how properties can make your code shorter, by adding some standard setter/getter

@interface Watch: NSObject

@property int hours;
@property int minutes;
-(void) printTime;

@end


@implementation Watch 

-(void) printTime
{
    NSLog(@"Time is %i:%i", _hours, _minutes);
}

@end

Also the naming convention of a getter is the property name

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        Watch *myWatch = [[Watch alloc]init];
        [myWatch setHours: 5];
        [myWatch setMinutes: 30];

        int hours = [myWatch hours]; //standard getter
        int minutes = [myWatch minutes];
        [myWatch printTime];
    }
    return 0;
}

naming conventions are important in Cocoa, as higher level technologies as Key-Value-Observing depends on them.

share|improve this answer
    
+1 for mentioning KVO. –  Nate May 24 '13 at 21:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.