• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie: Cannot use object as a parameter to a method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.


References: 
 >Re: Newbie: Cannot use object as a parameter to a method (From: "Michael S. Tashbook" <email@hidden>)

  • Prev by Date: Re: Newbie: Cannot use object as a parameter to a method
  • Next by Date: Re: Newbie: Cannot use object as a parameter to a method
  • Previous by thread: Re: Newbie: Cannot use object as a parameter to a method
  • Next by thread: Re: Newbie: Cannot use object as a parameter to a method
  • Index(es):
    • Date
    • Thread