取消引用空指针,但我没有使用指针
我在 xCode 中进行了“构建和分析”,并在我的 init 方法中将普通 int 设置为 0 时得到“取消引用空指针”。我在下面的代码中指出了我收到消息的行。我正在为 iPhone 进行开发。
Bric.m
#import "Bric.h"
@implementation Bric
- (id)initWithImage:(UIImage *)img:(NSString*)clr{
if (self = [super init]) {
image = [[UIImageView alloc] initWithImage:img];
}
stepX = 0; //It's for this line I get the message
stepY = 0;
oldX = 0;
color = [[NSString alloc]initWithString:clr];
visible = YES;
copied = NO;
return self;
}
@end
Bric.h
#import <Foundation/Foundation.h>
@interface Bric : NSObject {
int stepX;
int stepY;
}
-(id)initWithImage:(UIImage *)img:(NSString *)clr;
@end
不是完整的代码,粘贴了我认为有用的内容。
由于我没有使用指针,所以我觉得这很奇怪。我怎么会收到这个消息?
谢谢和问候, 尼克拉斯
I did the "Build and analyze" in xCode and get "Dereference of null pointer" when setting a normal int to 0 in my init-method. I noted in my code below for which row I get the message. I'm developing for iPhone.
Bric.m
#import "Bric.h"
@implementation Bric
- (id)initWithImage:(UIImage *)img:(NSString*)clr{
if (self = [super init]) {
image = [[UIImageView alloc] initWithImage:img];
}
stepX = 0; //It's for this line I get the message
stepY = 0;
oldX = 0;
color = [[NSString alloc]initWithString:clr];
visible = YES;
copied = NO;
return self;
}
@end
Bric.h
#import <Foundation/Foundation.h>
@interface Bric : NSObject {
int stepX;
int stepY;
}
-(id)initWithImage:(UIImage *)img:(NSString *)clr;
@end
It's not the complete code, pasted what I think is useful.
Since I am not using a pointer I find this quite strange. How come I get this message?
Thanks and Regards,
Niklas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
init 方法中的第一个
if
语句用于检查[super init]
是否返回nil
。 (从技术上讲,应该写成if ((self = [super init]))
,新的 LLVM 编译器会警告您)。静态分析器正在检查所有可能的代码路径,即使
[super init]
返回 nil 的情况也是如此。在这种情况下,您的if
语句失败,并且self
为nil
。如果self
为nil
则其实例变量不可访问。要解决此问题,您需要将初始化与图像初始化一起放置在
if
语句内,然后在 if 语句外部return self
。The first
if
statement in your init method is checking whether or not[super init]
returnsnil
. (Technically it should be writtenif ((self = [super init]))
, which the new LLVM compiler will warn you about).The static analyser is checking ALL possible code paths, even the case where
[super init]
returns nil. In this case, yourif
statement fails andself
isnil
. Ifself
isnil
then its instance variables aren't accessible.To fix this, you need to place your initialisations inside the
if
statement with the image initialisation and thenreturn self
outside the if statement.您已将其申报为财产吗?我不确定在这种情况下是否有必要,但您还没有创建访问器方法(尽管我认为您仍在直接设置实例变量...)
,即在您的头文件
和 .m 文件中,
这将允许您访问变量 self.stepX 和 self.stepY。
有时分析器会犯错误...我注意到它不能非常有效地处理
while
循环。不管怎样,看看如果你添加这些代码行并回复我会发生什么。Have you declared it as a property? I'm not sure if it's necessary in this case but you haven't made accessor methods (although I think you are still setting the instance variable directly...)
i.e., in your header file,
and in your .m file,
This will allow you to access the variable as self.stepX and self.stepY.
Sometimes the analyzer makes mistakes though... I noticed that it doesn't deal with
while
loops very effectively. Anyway, see what happens if you add those lines of code and get back to me.你的初始化方法是错误的。
它应该看起来像这样:
我假设您收到的消息来自静态分析器。由于 stepX 是一个实例变量,因此该行
实际上是
->
具有其正常 C 含义的简写。由于该行位于代码中 self 不为零的测试之外,因此静态分析器正在标记一个问题。Your init method is wrong.
It should look like this:
I assume the message you are getting is from the static analyser. As stepX is an instance variable, the line
is really shorthand for
where
->
has its normal C meaning. As that line is outside the test that self is non nil in your code, the static analyser is flagging an issue.