属性未在iOS 7中初始化 [英] Properties don't get initialized in iOS 7
问题描述
我正在为iOS 7开发,但是我仍然必须手动编写getter,否则我的属性就不会初始化.我试着手动合成那些属性,即使现在不再需要它了,但这也没有实现.
I'm developing for iOS 7 but I still have to manually write getters otherwise my properties just don't get initialized. I tried to manually synthesize those properties, even though that shouldn't be needed anymore, but that doesn't do it.
在下面的视图控制器中,我使用属性motionTracker
,该属性从未初始化.我所有的项目都有相同的问题,所以我知道这是我的误解.
In my view controller below, I use the property motionTracker
, which never gets initialized. I have the same issue with all my projects, so I know it's a misunderstanding on my part.
#import "ViewController.h"
#import "TracksMotion.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *startRecording;
@property (weak, nonatomic) IBOutlet UIButton *stopRecording;
@property (strong, nonatomic) TracksMotion *motionTracker;
@end
@implementation ViewController
@synthesize motionTracker = _motionTracker;
- (void)startMyMotionDetect
{
[self.motionTracker startsTrackingMotion];
}
@end
motionTracker
具有用于方法startsTrackingMotion
的公共API,所以我不知道为什么这不起作用.
The motionTracker
has a public API for the method startsTrackingMotion
so I don't know why this doesn't work.
#import <Foundation/Foundation.h>
#import <CoreMotion/CoreMotion.h>
@interface TracksMotion : NSObject
- (void)startsTrackingMotion;
- (void)stopTrackingMotion;
@property (strong, nonatomic) CMMotionManager *motionManager;
@end
推荐答案
属性/实例变量不是为您神奇地初始化的.当你说:
Properties / instance variables are not magically initialized for you. When you say:
@property (strong, nonatomic) TracksMotion *motionTracker;
...您只是为实例变量保留内存空间(并通过@synthesize
或自动合成生成getter和setter方法).在您放置之前,这里没有实际的TracksMotion对象.您必须编写代码才能做到这一点.您必须创建或获取 TracksMotion实例,并在某个时候将其分配给self.motionTracker
,大概是在self
生命周期的早期(在这种情况下,这是一个ViewController实例).在运行执行此操作的代码之前,self.motionTracker
为零.
... you are just reserving memory space for an instance variable (and generating a getter and a setter method through @synthesize
or autosynthesis). There is no actual TracksMotion object there until you put one there. You must write code to do that. You must create or obtain a TracksMotion instance and assign it to self.motionTracker
at some point, presumably early in the life of self
(in this case, that's a ViewController instance). Until you run code that does that, self.motionTracker
is nil.
((您可能会感到困惑,因为它似乎自动初始化了出口 .例如,您已经获得@property (weak, nonatomic) IBOutlet UIButton *startRecording;
并且可以肯定的是,self.startRecording
是一个按钮.但是那是因为笔尖加载过程为您做了我要说的必须要做的事情:它从情节提要或.xib文件中创建一个按钮,并将其分配给该实例变量.)
(It is possible that you are being confused because it looks like outlets are automatically initialized. For example, you've got @property (weak, nonatomic) IBOutlet UIButton *startRecording;
And sure enough, self.startRecording
is a button. But that's because the nib-loading process does for you the very thing I'm saying you must do: it makes a button from the storyboard or .xib file, and assigns it to this instance variable.)
这篇关于属性未在iOS 7中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!