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