objc / cocoa 的类变量范围问题?
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在哪里声明/定义“正常”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.
接口
方法可以完全访问其成员变量。而且C
函数不是类的一部分,因此它无法访问任何类变量,除非它采用类实例作为参数。显然,
cFunctionMine
不是接口的一部分。所以它并不像ttv
那样发送消息hooHa
。interface
methods have full access to it's member variables. And theC
function is not part of the class and so it cannot access any class variables unless it takes an class instance as the argument.Clearly
cFunctionMine
is not part of the interface. So it does not whatttv
is to send the messagehooHa
.