I’m new to Objective-C and I have been working with simple programs, and I was wondering how you would synthesize an Objective-C object pointer. So if I had a simple header file like so:
//
// Rectangle.h
// Program 8
#import <Foundation/Foundation.h>
#import "XYPoint.h"
@interface Rectangle : NSObject
{
XYPoint *origin;
}
- (XYPoint *) origin; // getter
- (void) setOrigin: (XYPoint *)pt; // setter
@end
How do you synthesize the object pointer *origin with @property and @synthesize?
Like this in the .h interface:
Nonatomic is optional and can be removed, which would make the property thread-safe. Retain means that the reference count will be incremented, Copy is an alternative to this which copies and increments the reference count, Assign is an alternative which is not very safe for use with objects and more intended for primitive types.
In the .m implementation:
or
In case you were wondering what the _origin is doing, there is a great explanation of this in the answer to this question here.
Don’t forget to release your properties in dealloc (or viewDidUnload – obviously not in the class you are writing here, but if working with a view controller).