开发者

General Programming Question about properties, assign, retain, and declarations

开发者 https://www.devze.com 2023-02-03 06:45 出处:网络
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.

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
{


@property (nonatomic,readonly,retain) AVCaptureSession *session;

This is a property that is readonly externally. Internally, it will have a setter that retains the new value.

@property (nonatomic,assign) AVCaptureVideoOrientation orientation;

This is a property that does a simple assignment to store the new value (since you can't -copy or -retain primitives).

@property (nonatomic,readonly,retain) AVCaptureAudioChannel *audioChannel;

This is a property that is readonly externally. Internally, it will have a setter that retains the new value.

@property (nonatomic,assign) NSString *sessionPreset;

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 NSString properties, you generally want them to be copy unless you have a good reason against it.

In the implementation file:

@property (nonatomic,retain) AVCaptureSession *session;
@property (nonatomic,retain) AVCaptureDeviceInput *videoInput;
@property (nonatomic,retain) AVCaptureDeviceInput *audioInput;

These are used in conjunction with the properties declared in the header, except now it's readwrite. By declaring the version in the header as readonly, anyone using the class will not have access to the setSession: 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.

@synthesize session = _session;
@synthesize orientation = _orientation;

This means you want the compiler to generate the appropriate setters and getters for the session and orientation properties, and that you want those properties to store their values in the _session and _orientation instance variables, respectively.

@dynamic audioChannel;
@dynamic sessionPreset;
@dynamic focusMode;

This means that the implementations for the setters and getters will be provided at runtime. You usually don't use @dynamic properties yourself, other than the ones provided by the Core Data framework.


In this code, the session property is defined as readonly outside of the class and readwrite inside the class. By overriding the session property in an unnamed category before synthesizing it, a set accessor will also be synthesized, but the header file says it is readonly so other classes don't know about the set accessor. The category also defines two new properties which are not visible to other classes at all.

The retain and assign keywords tell the compiler how the accessor methods should work. Retain means that the set accessor should retain the value of the property, and the assign keyword tells it to set the property without retaining it. There is also copy, which copies the value and is often used to make sure a mutable value isn't set. The default is assign, but the compiler will issue a warning if nothing is specified for an object property.


Can you explain what you're confused about here? My only guess is you are confused about audioChannel and sessionPreset, as they have no ivars and are declared @dynamic (there's also focusMode, but I don't even see a @property declaration for that in the code you pasted).

In any case, I expect that if you read the rest of the code you'll find that there are getters for -audioChannel and -sessionPreset that have been written, as well as a setter for -setSessionPreset:. Assuming that's the case, then the @dynamic declarations are completely unnecessary. @dynamic is only necessary to tell the compiler that the methods will exist at run-time; if they exist at compile-time then you don't need any directive whatsoever.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号