Re: Newbie: Cannot use object as a parameter to a method
Re: Newbie: Cannot use object as a parameter to a method
- Subject: Re: Newbie: Cannot use object as a parameter to a method
- From: Chris Giordano <email@hidden>
- Date: Thu, 22 Apr 2004 15:38:39 -0400
Michael,
Try (NSString *) instead of (NSString) as the return type of your
rollAsString method. (See below.) In Objective-C, you have to be
explicit about passing references/pointers to objects (unlike Java).
Since "int" isn't an Objective-C class, it works just fine to pass back
it by value, but an object has to be passed around by reference.
Also, you might want to initialize currValue in the initializer of your
class, just for safety's sake. Not a big deal, but you certainly could
end up with an unexpected value for your roll if you accidentally get
the value before rolling. I've been presumptuous and included a sample
below, just in case.
chris
On Apr 22, 2004, at 3:04 PM, Michael S. Tashbook wrote:
[snip]
CWDie.h:
/* CWDie */
#import <Cocoa/Cocoa.h>
@interface CWDie : NSObject
{
int currValue;
}
- (void)roll;
- (int)rollAsInt;
// - (NSString)rollAsString;
- (NSString *)rollAsString;
@end
CWDie.m:
#import "CWDie.h"
@implementation CWDie
- (id) init
{
if (self = [super init])
{
[self roll];
}
return self;
}
- (void)roll
{
currValue = (rand() % 6) + 1;
NSLog(@"Rolled a %d.\n", currValue);
}
- (int)rollAsInt
{
return currValue; // XCode reports the error here
}
// - (NSString)rollAsString
- (NSString *)rollAsString
{ // XCode reports the error here
return @"foo"; // Just a placeholder statement for now
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.