Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I'm at the next part of the book but my code is not working. It says that I can take a line of code that looks like this [numGuess setNumber : input]; and do this instead numGuess.setNumber = input;

The first version works in the [] brackets, but the DOT Operator is not working and I get the error.... " REQUEST FOR MEMBER 'setNumber' IN SOMETHING NOT A STRUCTURE OR UNION.

Code:
#import "test_instance.h"

int input;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	test_instance *numGuess = [[ test_instance alloc]init];
	
	NSLog(@"Pick a number:");
	scanf("%i", &input);
	
	numGuess.setNumber = input;
	[numGuess print];
	
    [pool drain];
    return 0;
}
 
Catfish_man, thanks! I am still grasping at the terminology. Perhaps you can break it down a little for me. Here is all the code. When you say "Import the deceleration of the method" are you referring to the Methods in the @interface section? Can you please explain it in a little more detail please?

Code:
#import <Foundation/Foundation.h>

@interface test_instance : NSObject 
{
	
	int number;
}
@property int number;
-(void) print;
-(void) setNumber: (int) n;

@end
Code:
#import "test_instance.h"

@implementation test_instance
@synthesize number;

-(void) print
{
	NSLog(@" the number is %i", number);
}
-(void) setNumber: (int) n
{
	number = n;
}

@end
Code:
#import "test_instance.h"

int input;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	test_instance *numGuess = [[ test_instance alloc]init];
	
	NSLog(@"Pick a number:");
	scanf("%i", &input);
	
	numGuess.setNumber = input;
	[numGuess print];
	
    [pool drain];
    return 0;
}
 
Your code should be

Code:
numGuess.number = input;

as that is the property name you declared earlier (number).

The setXxxxxx convention is only for naming setters not properties. Dot syntax
takes your property name and knows how to address the correct method.
 
Thanks for the explanation. I understand what is happening now and why it did not work.

-Lars
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.