Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 您使用过的NSLog有哪些独特用途?_Objective C_Cocoa_Nslog - Fatal编程技术网

Objective c 您使用过的NSLog有哪些独特用途?

Objective c 您使用过的NSLog有哪些独特用途?,objective-c,cocoa,nslog,Objective C,Cocoa,Nslog,您是否有用于调试的NSLog的独特或特殊用途?我喜欢使用此格式进行调试 NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] ) 我在NSLog中使用一些宏来快速调试cocoa的NSPoint、NSSize和NSRect结构的内容: #

您是否有用于调试的NSLog的独特或特殊用途?

我喜欢使用此格式进行调试

NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )

我在NSLog中使用一些宏来快速调试cocoa的
NSPoint
NSSize
NSRect
结构的内容:

#define LogPoint(POINT) CMLog(@"%s: (%0.0f, %0.0f)",\
                              #POINT, POINT.x, POINT.y)

#define LogSize(SIZE) CMLog(@"%s: %0.0f x %0.0f",\
                            #SIZE, SIZE.width, SIZE.height)

#define LogRect(RECT) CMLog(@"%s: (%0.0f, %0.0f) %0.0f x %0.0f",\
                            #RECT, RECT.origin.x, RECT.origin.y,\
                            RECT.size.width, RECT.size.height)
这些可按如下方式使用:

LogPoint(somePoint);
LogSize(someSize);
LogRect(someRect);
它们产生以下输出:

somePoint: (100, 200)
someSize: 12 x 440
someRect: (120, 240) 326 x 74

打印当前方法签名或函数名。

这不是NSLog功能,但是与NSLog一起使用非常方便:您可以对对象使用%@占位符,并将显示它们的说明:

NSLog (@"The object is %@", someKindOfObjectWhichYouWantToDisplay);

例如,通过这种方式,您可以快速查看返回的对象。这是通过向“描述”选择器发送一个对象来实现的,当然,这个对象可以在您自己的对象中实现。

这里有一个可以让您缩进调试日志的某些部分,以使内容更具可读性:

// MyDebugStuff.h:
void MyLog_Indent();
void MyLog_Outdent();
void MyLog(NSString * format, ...);

// MyDebugStuff.m:
int logIndentLevel = 0;

void MyLog_Indent()  { logIndentLevel++; }
void MyLog_Outdent() { if (logIndentLevel > 0) { logIndentLevel--; } }

void MyLog(NSString * format, ...)
{
    va_list args;
    va_start(args, format);

    NSString * indentString = [[NSString stringWithString:@""]
                     stringByPaddingToLength:(2*LogIndentLevel)
                                  withString:@" "
                             startingAtIndex:0];

    NSLogv([NSString stringWithFormat:@"%@%@", indentString, format], args);

    va_end(args);
}
可以这样使用:

MyLog(@"Hello, world");
MyLog_Indent();
MyLog(@"Step 1");
MyLog(@"Step 2");
MyLog_Indent();
MyLog(@"Step 2a");
MyLog(@"Step 2b");
MyLog_Outdent();
MyLog(@"Step 3");
MyLog_Outdent();
MyLog(@"Goodbye, cruel world!");
并将产生:

Hello, world Step 1 Step 2 Step 2a Step 2b Step 3 Goodbye, cruel world! 你好,世界 第一步 步骤2 步骤2a 步骤2b 步骤3 再见,残酷的世界!
仅供参考,大多数非对象类型已经有了方便的函数:NSStringFromRect、NSStringFromPoint、NSStringFromSize、NSStringFromRange等,因此除非您需要控制精确的格式以简化您的#定义谢谢,先生。我以前从没听说过这些!您可以将前两行替换为该行,因为Xcode会在构建中自动为您定义Release:#ifndef Release。我更喜欢它,因为它只是少了一个#define=)
// MyDebugStuff.h:
void MyLog_Indent();
void MyLog_Outdent();
void MyLog(NSString * format, ...);

// MyDebugStuff.m:
int logIndentLevel = 0;

void MyLog_Indent()  { logIndentLevel++; }
void MyLog_Outdent() { if (logIndentLevel > 0) { logIndentLevel--; } }

void MyLog(NSString * format, ...)
{
    va_list args;
    va_start(args, format);

    NSString * indentString = [[NSString stringWithString:@""]
                     stringByPaddingToLength:(2*LogIndentLevel)
                                  withString:@" "
                             startingAtIndex:0];

    NSLogv([NSString stringWithFormat:@"%@%@", indentString, format], args);

    va_end(args);
}
MyLog(@"Hello, world");
MyLog_Indent();
MyLog(@"Step 1");
MyLog(@"Step 2");
MyLog_Indent();
MyLog(@"Step 2a");
MyLog(@"Step 2b");
MyLog_Outdent();
MyLog(@"Step 3");
MyLog_Outdent();
MyLog(@"Goodbye, cruel world!");
Hello, world Step 1 Step 2 Step 2a Step 2b Step 3 Goodbye, cruel world!