if i have a class named
@interface myClass : NSObject {
int firstnum;
int secondnum;
}
//an a method that looks like this
-(myClass *) myMethod (myClass *) f;
@end
what does that method return as? im used to seeing something like (int) myMethod knowing that it returns an integer. but when its returning an object such as the classes name here what can it possibly return? if you'd like ill write out the whole project im working/studying on. (some methods have been truncated to keep the question simple. but if you'd like ill post it let me know. thnx
#import <Foundatio开发者_如何转开发n/Foundation.h>
@interface Fraction : NSObject {
int numerator;
int denominator;
}
@property int numerator, denominator;
-(Fraction *) add: (Fraction *) f;
@end
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b * d)
result.numerator = numerator * f.denominator + denominator * f.numerator;
result.denominator = denominator * f.denominator;
[result reduce];
return result;
}
@end
The question might be: what is an object? An object is some memory set aside to hold a bunch of values, and it's associated with some methods to interact with those values. You very rarely want to copy that whole chunk of memory. Instead, you pass around pointers to the memory location. So when you pass in or return an object, you are actually just passing around a pointer to the object's location in memory. Pointers are basically just integer values, so that's what's being returned.
In most languages, you don't even have to think about this, and in fact you rarely have to do it in Objective-C either. Just remember that MyClass*
is the type of objects that are of class ´MyClass´ and that they respond to all messages that are declared on that class.
That method returns just what it claims to: a pointer to an instance of myClass
. In the case of your add:
method, it returns the pointer result
that you created.
Incidentally, unless you have Garbage Collection enabled, you should add [result autorelease]
to that method before returning; otherwise your result fraction will be around forever and you'll leak memory.
CocoaDevCentral's Objective-C Tutorial will help with that memory management, and also might get you more comfortable with methods returning pointers to objects.
it looks like you're working through a copy of the Programming in Objective-C book. In my copy, there is a section at the end of Chapter 13 (Underlying C Language Features) called "How Things Work". I think you are looking for "Fact #2: An Object Variable is Really a Pointer". In your case, the add method is returning a pointer to an instance of the Fraction class. (The asterisk, *, means pointer, the class name before it says what sort of thing it is pointing to) There is more about pointers earlier in the chapter if you don't mind skipping about.
I'm going to be picky and try and rewrite this a bit
#import <Foundation/Foundation.h>
@interface Fraction : NSObject {
// You should really use the recommended type instead of int
NSInteger numerator;
NSInteger denominator;
}
// Don't try and keep these on one line.
// Also, don't depend on default values for assign, retain, or copy when declaring
@property (assign, nonatomic) numerator;
@property (assign, nonatomic) denominator;
// declare the add: function which takes a pointer to a Fraction as a parameter
// and returns a pointer to the restulting fraction
- (Fraction *)add:(Fraction *)aFraction;
@end
#import "Fraction.h"
@implementation Fraction
// Again, don't try and keep these on one line.
// It's easier to visually note the number of synthesised properties matches
// the number of declared properties.
@synthesize numerator;
@synthesize denominator;
// Assume that there is an init function.
- (Fraction *)add:(Fraction *)aFraction
{
Fraction *result = [[Fraction alloc] init];
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b * d)
result.numerator = self.numerator * aFraction.denominator
+ denominator * aFraction.numerator;
result.denominator = self.denominator * aFraction.denominator;
[result reduce]; // I assume this is implemented.
return result; // Assuming garbage collection, otherwise use
// return [result autorelease];
}
@end
精彩评论