objc / cocoa 的类变量范围问题?

发布于 2024-11-02 19:35:42 字数 932 浏览 0 评论 0原文

使用 GCC 4 在 XCode 3.1.3 中进行编译,在 Leopard 10.5.8 下,以 10.5 作为我的目标...

我有一个接口,因此:

@interface testThing : NSObject { classContaininghooHa *ttv; }
@end

和一个实现,因此:

@implementation: testThing

- (void) instanceMethodMine
{
    [ttv hooHa]; // works perfectly, compiles, links, hooHa is invoked.
}

//    void cFunctionMine()
//    {
//        [ttv hooHa]; // compiler: 'ttv' undeclared (first use in this function) "
//    }

void stupidCFunctionMine((testThing *)whom) // whom is passed class 'self' when invoked
{
   [whom instanceMethodMine]; // compiles, links, works. :/
}

@end

现在,我的理解 - 明显有缺陷 - 是如果您声明了一个变量、类 ID 或其他变量,它对于类来说是私有的,但在类内,本质上是作为全局执行的,在其存在期间存储在分配的类实例中。

这就是 objc 方法的作用方式。

但在上面的 c 函数中,也是在类中编写的,变量似乎是不可见的。这对我来说没有意义,但确实如此。

有人可以向我解释一下发生了什么事吗?

当您这样做时,如何将其声明为实例变量,以便我可以在如上所示的类范围内以及方法内声明的 ac 函数中使用该方法?

非常赞赏洞察力。

Compiling in XCode 3.1.3 using GCC 4, under Leopard 10.5.8, with 10.5 as my target...

I have an interface, thus:

@interface testThing : NSObject { classContaininghooHa *ttv; }
@end

And an implementation, thus:

@implementation: testThing

- (void) instanceMethodMine
{
    [ttv hooHa]; // works perfectly, compiles, links, hooHa is invoked.
}

//    void cFunctionMine()
//    {
//        [ttv hooHa]; // compiler: 'ttv' undeclared (first use in this function) "
//    }

void stupidCFunctionMine((testThing *)whom) // whom is passed class 'self' when invoked
{
   [whom instanceMethodMine]; // compiles, links, works. :/
}

@end

Now, my understanding -- clearly flawed -- was that if you declared a variable, class ID or otherwise, it was private to the class, but within the class, is performed essentially as a global, stored in the allocated class instance for the duration of its existence.

That's how it acts for objc methods.

But in the c function above, also written within the class, the variable appears to be invisible. The doesn't make sense to me, but there it is.

Can someone explain to me what is going on?

While you're at it, how can I declare this as an instance variable so I can use the method within a c function declared within the class scope as shown above, as well as within methods?

Insight much appreciated.

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

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

发布评论

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

评论(2

你在我安 2024-11-09 19:35:42

在哪里声明/定义“正常”c 函数没有任何区别。它们不是类的一部分,它们只是普通的旧 C 函数。与班级没有任何联系。如果您确实不想使该函数成为真正的 Objective-C 方法,则传递它们所处理的实例是一种解决方法。

It doesn't make any difference where you are declaring/defining your "normal" c functions. They are not part of the class, they are just plain old c functions. No connection to the class whatsoever. Passing the instance they work on is a workaround if you really don't want to make this function a true objective-c method.

格子衫的從容 2024-11-09 19:35:42

接口方法可以完全访问其成员变量。而且 C 函数不是类的一部分,因此它无法访问任何类变量,除非它采用类实例作为参数。

void cFunctionMine()
{
    [ttv hooHa]; // compiler: 'ttv' undeclared (first use in this function) 
}

显然,cFunctionMine 不是接口的一部分。所以它并不像 ttv 那样发送消息 hooHa

当您这样做时,我如何将其声明为实例变量,以便我可以在如上所示的类范围内以及方法内声明的 ac 函数中使用该方法?

void cFunctionMine()
{
    // 1. Create an instance using alloc and init

    testThing *ttv = [ [testThing alloc] init ] ;
    [ttv hooHa] ; 

    // Now the above statement is valid. We have a valid instance to which 
    // message can be passed to.

    // .....

    [ ttv release ] ;
    // release the resources once you are done to prevent memory leaks.
}

interface methods have full access to it's member variables. And the C function is not part of the class and so it cannot access any class variables unless it takes an class instance as the argument.

void cFunctionMine()
{
    [ttv hooHa]; // compiler: 'ttv' undeclared (first use in this function) 
}

Clearly cFunctionMine is not part of the interface. So it does not what ttv is to send the message hooHa.

While you're at it, how can I declare this as an instance variable so I can use the method within a c function declared within the class scope as shown above, as well as within methods?

void cFunctionMine()
{
    // 1. Create an instance using alloc and init

    testThing *ttv = [ [testThing alloc] init ] ;
    [ttv hooHa] ; 

    // Now the above statement is valid. We have a valid instance to which 
    // message can be passed to.

    // .....

    [ ttv release ] ;
    // release the resources once you are done to prevent memory leaks.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文