取消引用空指针,警告

发布于 2024-12-23 12:09:00 字数 1180 浏览 2 评论 0原文

我在 xCode 中进行了“构建和分析”并得到“取消引用空指针”。我在下面的代码中指出了我收到消息的行。我正在为 iPhone 进行开发。

"PDFScrollView.h"

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

ENsightAppDelegate *appDelegate;
}

 @end

"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;

}

这不是完整的代码,粘贴了我认为有用的内容。

我觉得这很奇怪。我怎么会收到这个消息?

在此处输入图像描述

I did the "Build and analyze" in xCode and get "Dereference of null pointer" . I noted in my code below for which row I get the message. I'm developing for iPhone.

"PDFScrollView.h"

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

ENsightAppDelegate *appDelegate;
}

 @end

"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;

}

It's not the complete code, pasted what I think is useful.

I find this quite strange. How come I get this message?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

向日葵 2024-12-30 12:09:00

如果 self 为 nil,则无法访问“self”的实例变量。因此,您应该仅在 if 语句内引用这些变量。换句话说,仅当 self 不为零时。

- (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;
}

If self is nil, then you can't access instance variables of 'self'. So you should reference those variables only inside of the if statement. In other words, only if self is not nil.

- (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;
}
伪心 2024-12-30 12:09:00

appDelegate 是该类的实例变量吗?如果是这样,您应该在 if (self =....) 中执行此操作。与 [self addSubview...] 相同。基本上,如果该 if 语句失败,您没有可以使用的对象。

is appDelegate an instance var of the class? if so, you should be doing it in the if (self =..... same with the [self addSubview...]. basically if that if statement fails, you don't have an object to work with.

任谁 2024-12-30 12:09:00

appDelegate 实际上是 [self appDelegate],因此如果将 null 分配给 self,则会取消引用 null 指针。

无论如何 - self 指向当前对象,为什么要分配给它?您可以直接使用它,无需分配。

最后一件事 - 无论如何,您确实在 on-before-last 行中显式使用 self ,这里很明显它是空的:

[self addSubview:pdfView];

appDelegate is actually [self appDelegate], so if you assign null to self, you dereference null pointer.

Anyway - self points to the current object, why would you assign to it? You can use it directly without an assignment.

And last thing - you do use self explicitly in the on-before-last line anyway, and here it is clear that it is null:

[self addSubview:pdfView];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文