取消引用空指针,警告

Nag*_*one 2 iphone debugging code-analysis objective-c

我在xCode中进行了"构建和分析"并获得了"空指针的取消引用".我在下面的代码中注意到我收到消息的行.我正在为iPhone开发.

"PDFScrollView.h"

 #import "ENsightAppDelegate.h"
@interface PDFScrollView : UIScrollView <UIScrollViewDelegate> {

ENsightAppDelegate *appDelegate;
}

 @end
Run Code Online (Sandbox Code Playgroud)

"PDFScrollView.m"

 - (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {

    // Set up the UIScrollView
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.bouncesZoom = YES;
    //self.decelerationRate = UIScrollViewDecelerationRateFast;
    self.delegate = self;
    [self setBackgroundColor:[UIColor grayColor]];
    self.maximumZoomScale = 5;//200
    self.minimumZoomScale = 0.5;//.85
    self.userInteractionEnabled = YES;
    self.delaysContentTouches = YES;
    self.canCancelContentTouches = NO;

}
appDelegate =(ENsightAppDelegate *) [[UIApplication sharedApplication] delegate];
pdfView=[appDelegate GetLeaveView];
[self addSubview:pdfView];
return self;

}
Run Code Online (Sandbox Code Playgroud)

这不是完整的代码,粘贴我认为有用的东西.

我觉得这很奇怪.我怎么得到这个消息?

在此输入图像描述

Fir*_*eer 8

如果self为nil,则无法访问"self"的实例变量.所以你应该只在if语句中引用那些变量.换句话说,只有在自我不是零的情况下.

- (id)initWithFrame:(CGRect)frame {

   if ((self = [super initWithFrame:frame])) {

      // Set up the UIScrollView
      self.showsVerticalScrollIndicator = NO;
      // ... bunch of other properties set...
      // ... btw, better style here would be to set the ivars directly (in an initializer)

      appDelegate =(ENsightAppDelegate *) [[UIApplication sharedApplication] delegate];
      pdfView=[appDelegate GetLeaveView];
      [self addSubview:pdfView];
    }

    return self;
}
Run Code Online (Sandbox Code Playgroud)