Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Objective-C Diving Deeper - Classes, Properties and Methods Creating Methods!

YAO GANG
YAO GANG
3,330 Points

How can I write a method in one page to pass this code challenge? I write this method in one Cococlass and main.m

calculate.h

import <Foundation/Foundation.h>

@interface calculateTip : NSObject

@property (nonatomic) float subtotal;

  • (void)multiply;

@end

calculate.m

import "calculateTip.h"

@implementation calculateTip

  • (void)multiply{

    self.subtotal = self.subtotal * 0.2;

}

@end

main.m

import <Foundation/Foundation.h>

import "calculateTip.h"

calculateTip *result = [[calculateTip alloc]init]; [result multiply];

variable_assignment.mm
#calculate.h
#import <Foundation/Foundation.h>

@interface calculateTip : NSObject

@property (nonatomic) float subtotal;


- (void)multiply;


@end


#calculate.m
#import "calculateTip.h"

@implementation calculateTip

- (void)multiply{

    self.subtotal = self.subtotal * 0.2;

}



@end


#main.m  
#import <Foundation/Foundation.h>
#import "calculateTip.h"
calculateTip *result = [[calculateTip alloc]init];
[result multiply];

1 Answer

Andres Aguero
Andres Aguero
30,545 Points

Hi there,

You need to specify the return type as float not as void.

-(float) calculateTip: (float) subtotal {
  return subtotal * .2; 
}

Then you need to copy the method signature into the interface/header file to be able to use it on other files like this.

-(float) calculateTip: (float) subtotal;
YAO GANG
YAO GANG
3,330 Points

yes. Thanks a lot