Objective-C 语法之 Debug 表达式

时间:2022-08-12 19:27:06

main.m

 #import <Foundation/Foundation.h>
#import "TestClass.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *purposeInfo = @"拿到的信息不仅在Debug中有用,而且在selector的传递也很有用";
[TestClass testDebugKeyword:purposeInfo];
}
return ;
}

TestClass.h

 #import <Foundation/Foundation.h>

 @interface TestClass : NSObject
+ (void)testDebugKeyword:(NSString *)purposeInfo; @end

TestClass.m

 #import "TestClass.h"

 @implementation TestClass

 + (void)testDebugKeyword:(NSString *)purposeInfo {
NSMutableString *mStrInfo = [[NSMutableString alloc] initWithString:purposeInfo];
[mStrInfo appendString:@"\n\nDebug时一些系统预留定义词的用法如下:\n"];
[mStrInfo appendFormat:@"__func__: %s\n", __func__];
[mStrInfo appendFormat:@"__PRETTY_FUNCTION__: %s\n", __PRETTY_FUNCTION__];
[mStrInfo appendFormat:@"__LINE__: %d\n", __LINE__];
[mStrInfo appendFormat:@"__FILE__: %s\n", __FILE__]; [mStrInfo appendString:@"\n\nDebug时一些Core Foundation方法的用法如下:\n"];
[mStrInfo appendFormat:@"NSStringFromSelector(_cmd): %@\n", NSStringFromSelector(_cmd)];
[mStrInfo appendFormat:@"NSStringFromClass([self class]): %@\n", NSStringFromClass([self class])];
[mStrInfo appendFormat:@"[[NSString stringWithUTF8String:__FILE__] lastPathComponent]: %@\n", [[NSString stringWithUTF8String:__FILE__] lastPathComponent]]; NSLog(@"%@", mStrInfo);
} @end

结果:

 -- ::16.353 OCDebugKeyword[:] 拿到的信息不仅在Debug中有用,而且在selector的传递也很有用

 Debug时一些系统预留定义词的用法如下:
__func__: +[TestClass testDebugKeyword:]
__PRETTY_FUNCTION__: +[TestClass testDebugKeyword:]
__LINE__:
__FILE__: /Users/Kenmu/Documents/iOSDevelopment/OCDebugKeyword/OCDebugKeyword/TestClass.m Debug时一些Core Foundation方法的用法如下:
NSStringFromSelector(_cmd): testDebugKeyword:
NSStringFromClass([self class]): TestClass
[[NSString stringWithUTF8String:__FILE__] lastPathComponent]: TestClass.m