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

drekka

macrumors newbie
Original poster
Jan 1, 2009
5
0
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:

Code:
#import <Foundation/Foundation.h>

@interface MyClass : NSObject {
@private
	NSString *someText;
}

@property (retain) NSString *someText;
@end

I then coded the class like this:

Code:
#import "MyClass.h"

@implementation MyClass
@synthesize someText;
@end

And finally modified the drawRect method of my extension to the UIView class to look like this:

Code:
- (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?

ciao
Derek
 

ghayenga

macrumors regular
Jun 18, 2008
190
0
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:

Code:
#import <Foundation/Foundation.h>

@interface MyClass : NSObject {
@private
	NSString *someText;
}

@property (retain) NSString *someText;
@end

I then coded the class like this:

Code:
#import "MyClass.h"

@implementation MyClass
@synthesize someText;
@end

And finally modified the drawRect method of my extension to the UIView class to look like this:

Code:
- (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?

ciao
Derek

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.
 

drekka

macrumors newbie
Original poster
Jan 1, 2009
5
0
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:

Code:
- (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. :)

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