標籤:
main.m
1 #import <Foundation/Foundation.h> 2 #import "TestClass.h" 3 4 int main(int argc, const char * argv[]) { 5 @autoreleasepool { 6 NSString *purposeInfo = @"拿到的資訊不僅在Debug中有用,而且在selector的傳遞也很有用"; 7 [TestClass testDebugKeyword:purposeInfo]; 8 } 9 return 0;10 }
TestClass.h
1 #import <Foundation/Foundation.h>2 3 @interface TestClass : NSObject4 + (void)testDebugKeyword:(NSString *)purposeInfo;5 6 @end
TestClass.m
1 #import "TestClass.h" 2 3 @implementation TestClass 4 5 + (void)testDebugKeyword:(NSString *)purposeInfo { 6 NSMutableString *mStrInfo = [[NSMutableString alloc] initWithString:purposeInfo]; 7 [mStrInfo appendString:@"\n\nDebug時一些系統預留定義詞的用法如下:\n"]; 8 [mStrInfo appendFormat:@"__func__: %s\n", __func__]; 9 [mStrInfo appendFormat:@"__PRETTY_FUNCTION__: %s\n", __PRETTY_FUNCTION__];10 [mStrInfo appendFormat:@"__LINE__: %d\n", __LINE__];11 [mStrInfo appendFormat:@"__FILE__: %s\n", __FILE__];12 13 [mStrInfo appendString:@"\n\nDebug時一些Core Foundation方法的用法如下:\n"];14 [mStrInfo appendFormat:@"NSStringFromSelector(_cmd): %@\n", NSStringFromSelector(_cmd)];15 [mStrInfo appendFormat:@"NSStringFromClass([self class]): %@\n", NSStringFromClass([self class])];16 [mStrInfo appendFormat:@"[[NSString stringWithUTF8String:__FILE__] lastPathComponent]: %@\n", [[NSString stringWithUTF8String:__FILE__] lastPathComponent]];17 18 NSLog(@"%@", mStrInfo);19 }20 21 @end
結果:
1 2015-06-20 11:07:16.353 OCDebugKeyword[465:18859] 拿到的資訊不僅在Debug中有用,而且在selector的傳遞也很有用 2 3 Debug時一些系統預留定義詞的用法如下: 4 __func__: +[TestClass testDebugKeyword:] 5 __PRETTY_FUNCTION__: +[TestClass testDebugKeyword:] 6 __LINE__: 18 7 __FILE__: /Users/Kenmu/Documents/iOSDevelopment/OCDebugKeyword/OCDebugKeyword/TestClass.m 8 9 10 Debug時一些Core Foundation方法的用法如下:11 NSStringFromSelector(_cmd): testDebugKeyword:12 NSStringFromClass([self class]): TestClass13 [[NSString stringWithUTF8String:__FILE__] lastPathComponent]: TestClass.m
Objective-C 文法之 Debug 運算式