我正在使用 TestFlight,并且我用这个宏来替换 NSLog 等 TestFlight 远程日志记录。
#define NSLog(__FORMAT__, ...) TFLog((@"%s [Line %d] " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
我刚刚从 TestFlight 网站复制粘贴了此内容,现在我想出于稍微不同的目的创建自己的内容。
我希望能够输入...
MyEventLog(@"Something happened.");
...并将其解释为...
[[MyEventLogManager sharedInstance] newLogWithText:@"Something happened"];
我只是不确定语法是如何工作的。
最佳答案
#define MyEventLog(message) [[MyEventLogManager sharedInstance] newLogWithText:message]
但是,您会发现提供该方法的可变参数版本很有用,因此您可以向其传递格式化文本:
- (void)newLogWithFormat:(NSString *)format, ...
{
va_list va;
va_start(va, format);
NSString *message = [[NSString alloc] initWithFormat:format arguments:va];
va_end(va);
[self newLogWithText:message];
// If not using ARC, then:
// [message release];
}
并使用:
#define MyEventFormat(__FORMAT__, ...) [[MyEventLogManager sharedInstance] newLogWithFormat:__FORMAT__, ##__VA_ARGS__]
关于ios - 定义一个宏来替换代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14999608/