The Dot Operator

Discussion in 'Mac Programming' started by larswik, Aug 20, 2010.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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;
    }
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    You need to import the declaration of the method. The dot operator enforces typesafety more strictly.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    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;
    }
    
     
  4. yoavcs macrumors regular

    Joined:
    Apr 7, 2004
    Location:
    Israel
    #4
    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.
     
  5. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    Thanks for the explanation. I understand what is happening now and why it did not work.

    -Lars
     

Share This Page