UIView drawRect;类变量超出范围 [英] UIView drawRect; class variables out of scope

查看:204
本文介绍了UIView drawRect;类变量超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短&



我有一个UIVIew与一个init和一个drawrect方法(和另一个线程和一堆其他东西,但是,



在 - (id)init方法中分配的所有类变量和init都超出范围/ nil / p>

例如:



在接口:

  NSObject * fred; 

在实施中:

   - (id)init 
{
if(self == [super init])
{
fred = [[NSObject alloc] init];
}

return self;
}

- (void)drawRect:(CGRect)rect
{
NSLog(@Fred保留%i次,[fred retainCount]); // FAIL
NSLog(@但是他的变量实际上只是指向未初始化的0x0,所以你不会在调试器中读取这个,因为应用程序在它到达之前已经崩溃了。
}

应该在drawRect之前调用init IS。任何人都有什么想法?

解决方案

它实际上是调用 NSLog












编辑:一些简短的测试显示,问题不是fred未初始化;如果是;你不会收到任何错误,因为发送消息到 nil 不会导致崩溃(实例变量被初始化为 nil )发生了什么事实, code> fred 被释放;导致指针指向垃圾,而不是一个对象(或者更确切地说,一个乱码或乱码的对象,不再能接收消息。)



您提供的代码不足以找到错误;你将不得不粘贴更多(和应用程序中使用的确切代码)。


Short & sweet version of my last question in light of new information.

I have a UIVIew with an init and a drawrect method (and another thread and a bunch of other stuff, but I'll keep it short & sweet).

All of the class variables that I alloc and init in the -(id)init method are out of scope/nil/0x0 in the drawRect method, and I am unable to access them.

For example;

In the interface:

NSObject* fred;

In the implementation:

-(id)init
{
    if(self == [super init])
    {
        fred = [[NSObject alloc] init];
    }

    return self;
}

-(void)drawRect:(CGRect)rect
{
    NSLog(@"Fred is retained %i times",[fred retainCount]); //FAIL
    NSLog(@"But his variable is actually just pointing at uninitialised 0x0, so you're not reading this in the debugger because the application has crashed before it got here."
}

Should add that init IS being called before drawRect also. Anyone have any ideas?

解决方案

IS it? Have you actually put a call to NSLog in there and checked? Because I would think it's initialized with initWithFrame: or something to that effect.

Edit: Some brief testing reveals that the issue isn't that fred is uninitialized; if it was; you'd not get any error, as sending messages to nil will not cause a crash. (Instance variables are initialized to nil). What is happening, in fact, is that somewhere along the line fred is released; causing the pointer to point to garbage, rather than an object. (Or rather, a garbled or garbaged object that can no longer receive messages.)

The code you have provided is not enough to find the error; you're going to have to paste more (and the exact code used in your application, too).

这篇关于UIView drawRect;类变量超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆