Newbie question about setting a property via dot syntax. Can't get it to work :-)

Discussion in 'iOS Programming' started by drekka, Jan 1, 2009.

  1. macrumors newbie

    Jan 1, 2009
    Hello everyone,
    I'm a long time Java developer coming across to the iPhone and Objective C. This is my first day of coding :)

    I started with the basic hello world app which uses a UIView to write some text on the screen. I then decided to implement a standalone class of my own design and see how to access it and get information from it. So I implemented an interface like this:

    #import <Foundation/Foundation.h>
    @interface MyClass : NSObject {
    	NSString *someText;
    @property (retain) NSString *someText;
    I then coded the class like this:

    #import "MyClass.h"
    @implementation MyClass
    @synthesize someText;
    And finally modified the drawRect method of my extension to the UIView class to look like this:

    - (void)drawRect:(CGRect)rect {
        // Drawing code
    	CGPoint location = CGPointMake(10,20);
    	UIFont *font = [UIFont systemFontOfSize:24];
    	MyClass *myClass = [[MyClass alloc] init];
    	myClass.someText = @"hello again";
    	[[UIColor redColor] set];
    	[[myClass someText] drawAtPoint:location withFont:font];
    	[myClass release];
    When I run this though, I get the message "Error:request for member 'someText' in something not a structure or union."

    If I change it to [myClass setSomeText:mad:"hello again"]; then everthing works fine and "hello again" is drawn on the screen.

    I've done some reading and even found the Objective C example on wikipedia which appears to be practically identical to my code. It sounds to me that the myClass reference(pointer) is not being seen as type MyClass and therefore the compiler will not compile the code. But everything looks fine and matches the code examples I've been looking at.

    Any ideas what I'e stuffed up?

  2. macrumors regular

    Jun 18, 2008
    Yup. You've declared someText to be private, with the @private. As a private property it can only be accessed through it's getters and setters, which are implicit methods, and thus must use the obj_msgSend syntax. If you remove the @private designation it should work fine.
  3. thread starter macrumors newbie

    Jan 1, 2009
    Thanks, that fixed it, but it's got me a little confused.

    @synthesize should be generating the two accessor methods. Probably something like this if I understand it correctly:

    - (void)setSomeText:(NSString *)newValue;
    - (NSString *) someText();
    What I don't understand is why changing from @private to @pblic makes this work. I was expecting that the two accessors would be public and that the dot syntax code would use the accessors. At least thats how I've read the material I've seen so far. :)


Share This Page