Reputation: 6693
I've got next to no knowledge about Objective-C. I'm working with an Obj-C framework for an AVCaptureSession. I need to have access to the AVCaptureVideoDataOutput of the camera's session.
In the .m file the session is initialized and set up.
@property (nonatomic, strong) AVCaptureSession *session;
I cannot move the property from the .m to .h file because the .m file needs it for all of the functions to set it up.
I also tried making them readwrite / readonly in the .m and .h respectively to avoid a redeclaration issue, but then when I call on the camera.session after I'm starting the camera up, "session" returns nil and the app crashes.
I don't know if there would be any problem just moving all of the code into the .h file so it's all publicly accessable?
How can I access the session property from a .m file in my View Controller?
Upvotes: 2
Views: 311
Reputation: 2382
Moving the property to the .h
. exposes it publicly and also makes it available to the .m
because the .m
file by default imports the .h
.
Example:
// CameraViewController.h
@interface CameraViewController : UIViewController
@property (nonatomic, strong) AVCaptureSession *session;
@end
// CameraViewController.m
#import "CameraViewController.h" // <- notice the import, `session` is accessible internally.
As for the session
property being nil it may be because you have not initialized it with a value yet. In your init
you may want to initialize session
with [[AVCaptureSession alloc] init]
Note: to avoid potential problems with unintended mutability I recommend asking your self if you can make publicly exposed properties read only i.e.
@property (nonatomic, strong, readonly) AVCaptureSession *session;
Upvotes: 2
Reputation: 8144
If you move it to the .h the .m has access to it as well. You can even make it readonly in the header and readwrite in the m
So do either:
.h:
@property (nonatomic, strong) AVCaptureSession *session;
Or:
.h:
@property (nonatomic, strong, readonly) AVCaptureSession *session;
.m:
@property (nonatomic, strong, readwrite) AVCaptureSession *session;
Upvotes: 1