“在非成员函数中使用'this'无效”在客观方面? [英] "Invalid use of 'this' in non-member function" in objective-c context?
问题描述
使用Xcode。
在此代码中(func在接口中声明),告诉subj错误,站在带有'self'的字符串上。
In this code (func is declared in interface), tells subj error, standing on string with 'self'.
+ (void) run: (Action) action after: (int) seconds
{
[self run:action after:seconds repeat:NO];
}
什么......?
推荐答案
self
是一个实例变量,用于引用当前对象的实例。
self
is an instance variable used to refer to an instance of the current object.
您正试图在类级别方法中使用它 +(void)...
其中 self
没有意义。尝试使用共享实例,或将有问题的类的实例传递给方法。
You are attempting to use it in a class level method +(void)...
where self
has no meaning. Try using a shared instance, or passing an instance of the class in question to the method.
+ (void) run:(Action)action on:(MyClass*) instance after:(int) seconds
{
[instance run:action after:seconds repeat:NO];
}
编辑
我的评论者指出 self
在类级别上下文中确实有意义,但它指的是类本身。这意味着你试图调用一个如下所示的方法:
My commenters have pointed out that self
does have meaning in class level contexts, but it refers to the class itself. That would mean you were trying to call a method that looks like this:
[MyClass run:action after:seconds repeat:NO];
您的目标应该是:
[myClassInstance run:action after:seconds repeat:NO];
这篇关于“在非成员函数中使用'this'无效”在客观方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!