• 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: Dave Rosborough <email@hidden>
  • Date: Thu, 22 Apr 2004 12:44:44 -0700

Your problem is that in objective-C, you don't return objects from your methods, you return pointers to objects. Therefore, your method should read something like this:

- (NSString *)rollAsString
{
return @"foo";
}

That little asterisk is important! It should fix up both of your errors.

Don't forget to make the fix in your header file too!

TTYL
DaveR


On 22-Apr-04, at 12:04 PM, Michael S. Tashbook wrote:

On 22 Apr 2004, at 19:07, Michael S. Tashbook wrote:
I'm trying to create a simple class that represents a six-sided die.
XCode keeps complaining about my rollAsInt method, which is just
supposed to return the value of the currValue instance variable (the
code below omits a similar method that returns the value as an
NSString,
which gives the same error message).

Hmmm... That's odd. Everything seems to work fine when I comment out the
*other* method in my class; I had previously omitted it because it
generated the same error message, and I figured that there was something
more general to blame.

Here are my two class source code files, in their entirety. When I
uncomment the rollAsString method, I get the original "Can not use an
object as a parameter to a method" error message.

As a side note, XCode also reports that rollAsString has an incompatible
return type. According to the O'Reilly book, the @"" form should
automatically create an NSString object, which is what the method is
declared to return. Did I miss a memo somewhere? ;)

Any ideas?

Mike


CWDie.h:

/* CWDie */

#import <Cocoa/Cocoa.h>

@interface CWDie : NSObject
{
int currValue;
}

- (void)roll;
- (int)rollAsInt;
- (NSString)rollAsString;

@end


CWDie.m:

#import "CWDie.h"

@implementation CWDie
- (void)roll
{
currValue = (rand() % 6) + 1;
NSLog(@"Rolled a %d.\n", currValue);
}

- (int)rollAsInt
{
return currValue; // XCode reports the error here
}


- (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.
_______________________________________________
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: Loading/Unloading a Cocoa-using framework from a CFM application
  • Next by Date: Re: Loading/Unloading a Cocoa-using framework from a CFM application
  • 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