Amazon.com Widgets about "setTo" and "over"
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 24, 2014, 03:25:43 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Program Examples
| | |-+ Chapter 7
| | | |-+ about "setTo" and "over"
Pages: [1] Go Down
Print
Author Topic: about "setTo" and "over" (Read 1100 times)
zhangfeng
Newbie
*
Posts: 2






on: October 03, 2010, 11:13:41 PM

Hello.

I'm reading the second edition of "Programming in Objective-C 2.0". The following code in "Synthesized Accessor Methods" of Chapter 7 confused me.

Code: (Objective-C)
@interface Fraction : NSObject
{
int numerator;
int denominator;
}

@property int numerator, denominator;

-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
@end

I have 2 questions:

1, What's the meaning of setTo and over? I deleted "-(void) setTo: (int) n over : (int) d", and the program works well.

2, If there are more than 2 instance variables, how do we rewrite this sentence?

Thanks!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: October 04, 2010, 07:53:59 AM

You'll recall that setTo:Over: was a method we developed that allowed you to set a fraction's numerator and denominator in a single method call.   There is no way to synthesize a method for more than one property (that is, a method that takes multiple arguments).

Cheers,

Steve Kochan
Logged
zhangfeng
Newbie
*
Posts: 2






Reply #2 on: October 04, 2010, 08:56:50 PM

You'll recall that setTo:Over: was a method we developed that allowed you to set a fraction's numerator and denominator in a single method call.   There is no way to synthesize a method for more than one property (that is, a method that takes multiple arguments).

Cheers,

Steve Kochan

Thanks for your answer.

If I don't want to set a fraction's numerator and denominator in a single method call, can  setTo: over: be deleted from the program like this?

Fraction.h
Code: (Objective-C)
#import <Foundation/Foundation.h>


@interface Fraction : NSObject {
int numerator;
int denominator;
}

@property int numerator, denominator;

-(void) print;
//-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
@end

Fraction.m
Code: (Objective-C)
#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;

-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}

-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}
@end

FractionTest.m
Code: (Objective-C)
#import "Fraction.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Fraction *myFraction = [[Fraction alloc] init];

//set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

//display the fraction
NSLog(@"The value of myFraction is:");
[myFraction print];
[myFraction release];

    [pool drain];
    return 0;
}

And sorry for my bad English. I can't understand "There is no way to synthesize a method for more than one property (that is, a method that takes multiple arguments).". In this example, we can set 2 instance variables using 1 method. Why we can't synthesize a method for more than property?



Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.