I was going through the AVCam project from WWDC and I’m curious about the following code. I thought you were supposed to declare an object, then @property (nonatomic,retain), then synthesize.
The Demo code does it a little differently, I’ll post some code (just a sample), anyone know what this does and when you should use it? Can anyone explain its significance and when to use it?
@interface AVCamCaptureManager : NSObject {
@private
// Capture Session
AVCaptureSession *_session;
AVCaptureVideoOrientation _orientation;
// Identifiers for connect/disconnect notifications
id _deviceConnectedObserver;
id _deviceDisconnectedObserver;
}
@property (nonatomic,readonly,retain) AVCaptureSession *session;
@property (nonatomic,assign) AVCaptureVideoOrientation orientation;
@property (nonatomic,readonly,retain) AVCaptureAudioChannel *audioChannel;
@property (nonatomic,assign) NSString *sessionPreset;
in the implementation file:
@interface AVCamCaptureManager ()
@property (nonatomic,retain) AVCaptureSession *session;
@property (nonatomic,retain) AVCaptureDeviceInput *videoInput;
@property (nonatomic,retain) AVCaptureDeviceInput *audioInput;
@end
@implementation AVCamCaptureManager
@synthesize session = _session;
@synthesize orientation = _orientation;
@dynamic audioChannel;
@dynamic sessionPreset;
@dynamic focusMode;
- (id) init
{
This is a property that is
readonlyexternally. Internally, it will have a setter that retains the new value.This is a property that does a simple assignment to store the new value (since you can’t
-copyor-retainprimitives).This is a property that is
readonlyexternally. Internally, it will have a setter that retains the new value.This is a property that does a simple assignment to store the new string value. This is normally not a good idea, unless you’re only allowing pre-defined constants for the presets. When dealing with
NSStringproperties, you generally want them to becopyunless you have a good reason against it.In the implementation file:
These are used in conjunction with the properties declared in the header, except now it’s
readwrite. By declaring the version in the header asreadonly, anyone using the class will not have access to thesetSession:method. We re-declare this property internally so that we can have access to the setter (and the setter retains the new value). Also, if the property is not present in the header, the user won’t know it exists, but we’ll still be able to use it internally.This means you want the compiler to generate the appropriate setters and getters for the
sessionandorientationproperties, and that you want those properties to store their values in the_sessionand_orientationinstance variables, respectively.This means that the implementations for the setters and getters will be provided at runtime. You usually don’t use
@dynamicproperties yourself, other than the ones provided by the Core Data framework.