Jef*_*eff 4 iphone xcode objective-c ios ios5
我读了这篇文章:http://weakreference.wordpress.com/2011/06/22/overriding-nslog-on-ios/.
本文的想法是将这两个内容添加到应用程序的prefix.pch文件中,以便您可以覆盖NSLog的行为.
我要添加的两件事是:
#define NSLog(...) customLogger(__VA_ARGS__);
Run Code Online (Sandbox Code Playgroud)
和
void customLogger(NSString *format, ...) {
va_list argumentList;
va_start(argumentList, format);
NSMutableString * message = [[NSMutableString alloc] initWithFormat:format
arguments:argumentList];
[message appendString:@"Our Logger!"]; // Our custom Message!
NSLogv(message, argumentList); // Originally NSLog is a wrapper around NSLogv.
va_end(argumentList);
[message release];
}
Run Code Online (Sandbox Code Playgroud)
xCode抛出错误匹配错误,它会找到customLogger的重复项.
有没有人成功覆盖NSLog?
谢谢!
编辑以回应Rob:
好,太棒了.我们正在取得进步!我就像你问的那样动了东西.这是我们现在拥有的:
我的自定义记录器:
void customLogger(NSString *format, ...) {
va_list args;
va_start(args, format);
va_end(args);
[newLogger log:format withArgs:args];
}
//This is a newLogger Method
+ (void) log:(NSString *)format withArgs:(va_list) args{
NSArray *occ = [format componentsSeparatedByString:@"%@"];
NSInteger characterCount = [occ count];
NSArray *stringItems = [format componentsSeparatedByString:@"%@"];
NSMutableString *tmp = [[NSMutableString alloc] initWithFormat: @"%@",[stringItems objectAtIndex:0]];
for( int i = 1; i < characterCount; i++ ) {
NSString *value = va_arg(args, NSString *);
[tmp appendString:value];
[tmp appendString:[stringItems objectAtIndex:i]];
}
// Need to alter the above and actually do something with the args!
[tmp appendString:@"\n"];
[[newLogger sharedInstance].logBuffer appendString:tmp];
if ([newLogger sharedInstance].textTarget){
[[newLogger sharedInstance].textTarget setText:sharedInstance.logBuffer];
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用+ log时,我在线程1上收到SIBABRT错误.
听起来就像你customLogger在.pch文件中定义的那样.这意味着每个.m文件都包含它,因此.o您的项目创建的每个文件都包含它自己的副本customLogger.这就是您从链接器获得重复的符号定义错误的原因.
你需要在声明customLogger中声明.pch,如下所示:
void customLogger(NSString *format, ...);
Run Code Online (Sandbox Code Playgroud)
并创建一个customLogger.m包含定义的文件.