取消引用空指针,警告
我在 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 self 为 nil,则无法访问“self”的实例变量。因此,您应该仅在 if 语句内引用这些变量。换句话说,仅当 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.
appDelegate
是该类的实例变量吗?如果是这样,您应该在if (self =....
) 中执行此操作。与[self addSubview...]
相同。基本上,如果该 if 语句失败,您没有可以使用的对象。is
appDelegate
an instance var of the class? if so, you should be doing it in theif (self =....
. same with the[self addSubview...]
. basically if that if statement fails, you don't have an object to work with.appDelegate
实际上是[self appDelegate]
,因此如果将 null 分配给self
,则会取消引用 null 指针。无论如何 -
self
指向当前对象,为什么要分配给它?您可以直接使用它,无需分配。最后一件事 - 无论如何,您确实在 on-before-last 行中显式使用
self
,这里很明显它是空的:appDelegate
is actually[self appDelegate]
, so if you assign null toself
, 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: