简体   繁体   English

目标 C class 方法错误“使用未声明的标识符”

[英]Objective C class method error ''Use of undeclared identifier"

Hopefully not a ridiculous question as I'm a self taught programmer, but I found some code which looked like it might help me do something I wanted to.希望这不是一个荒谬的问题,因为我是一名自学成才的程序员,但我发现了一些看起来可以帮助我做我想做的事情的代码。 Basically I want to enhance the NSLog() function in one of my apps, and would like to call my method in the same way as NSLog().基本上我想在我的一个应用程序中增强 NSLog() function,并且想以与 NSLog() 相同的方式调用我的方法。 It's very difficult to google 'void function()' because nothing seems to be relevant, I'm used to methods that are constructed like this: google 'void function()' 非常困难,因为似乎没有什么是相关的,我习惯了这样构造的方法:

-(void)RLog(NSString*)statement;

But the sample suggested using:但样本建议使用:

void RLog(NSString *statement,...);

This seems to work, and lets me call it with 'RLog(@"My message")', however using it this way the method ignores the class variables, and seems I can't use 'self' either...这似乎可行,让我用'RLog(@“My message”)'调用它,但是以这种方式使用它,该方法会忽略 class 变量,而且似乎我也不能使用'self'...

Logging.h日志记录.h

#import <UIKit/UIKit.h>
@import Foundation.NSString;

@interface Logging : NSObject

@property (nonatomic) BOOL enabled;

void RLog(NSString *statement,...);

@end

Logging.m日志记录.m

#import "Logging.h"

@implementation Logging

@synthesize enabled;

void RLog(NSString *format,...) {

     /* something that uses 'enabled' throws 'Use of undeclared identifier enabled' */

     /* something else that uses '[[self class] session]' throws 'Use of undeclared identifier self' */

}

@end

Hopefully I've included enough information to explain my issue, and not removed too much to make it hard to understand!希望我已经包含了足够的信息来解释我的问题,并且没有删除太多以使其难以理解!

Thanks in advance.提前致谢。

Plasma等离子体

void RLog(NSString *format,...)

... is not an Objective-C method ; ...不是 Objective-C方法 it's just a C function .它只是一个 C function

C functions are global and free-standing. C 函数是全局且独立的。 There is no self here and no connection with Logging's enabled .这里没有self ,并且与 Logging 的enabled没有任何联系。 As far as this function is concerned, the Logging class might as well not exist at all.就这个 function 而言,Logging class 还不如根本不存在。

After a bit more investigation I found a way around this using the information here .经过更多调查后,我使用此处的信息找到了解决此问题的方法。

I added a class variable called thisClass and then assigned 'self' to it during my class init stage.我添加了一个名为 thisClass 的 class 变量,然后在我的 class 初始化阶段将“self”分配给它。

@implementation Logging
    
id  thisClass;

- (id) init
{
    if (self = [super init])
    {
        thisClass = self;
    }
    return self;
}

From inside the C function I can now call methods in my Logging class using:从 C function 内部,我现在可以使用以下方法调用我的 Logging class 中的方法:

[thisClass log:@"Testing"];

Without having to send "self" in as a parameter, I've moved the rest of the code which needs the local variables in to my 'log' method and simply use the C method to call it in a simplified way.无需将“self”作为参数发送,我将需要局部变量的代码的 rest 移动到我的“log”方法中,并简单地使用 C 方法以简化方式调用它。

Plasma等离子体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM