gpt4 book ai didi

objective-c - 在 Xcode 中重新定义 NSLog

转载 作者:行者123 更新时间:2023-12-04 03:13:44 25 4
gpt4 key购买 nike

我有一个关于 Xcode 和 Objective-C 的问题。

我想在 Xcode 中做这个简单的 Action :

如果我输入这个:

NSLog(@"something else");

我希望 Xcode 编写(或编译后执行):

NSLog(@"[%@] something else", NSStringFromClass([self class]));

当我键入 NSLog... 时,Xcode 4 可能会在自动完成菜单中提出建议...

最佳答案

这是一组完整的 Log #define 指令(包括 ULog,一个基于 UIAlertView 的日志记录功能)

// DLog will output like NSLog only when the DEBUG variable is set

#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...)
#endif

// ALog will always output like NSLog

#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);

// ULog will show the UIAlertView only when the DEBUG variable is set

#ifdef DEBUG
# define ULog(fmt, ...) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }
#else
# define ULog(...)
#endif

作者 Ciryon ,只需将它们放在您的预编译头文件 (.pch) 中即可。

(来源:http://overbythere.co.uk/blog/2012/01/alternatives-nslog)

关于objective-c - 在 Xcode 中重新定义 NSLog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5866126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com