Xcode 中的 NSObject 描述和自定义摘要

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18090580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:40:13  来源:igfitidea点击:

NSObject description and custom summaries in Xcode

objective-cxcodelldb

提问by Ben Sinclair

I override object's -(NSString*)descriptionhowever Xcode always displays error: summary string parsing errorin summary field in variables view.

我覆盖了对象,-(NSString*)description但是 Xcode 总是显示error: summary string parsing error在变量视图的摘要字段中。

My current implementation is the following:

我目前的实现如下:

- (NSString*)description {
    return [NSString stringWithFormat:@"<%@ %p> x=%f, y=%f", self.class, self, _x, _y];
}

If I type po objectNamein console, LLDB shows a fine output as expected, however Xcode and command p objectNamealways indicate error, so what's the proper debug description format to make summary field work? Worth to notice that the output of "p" command is the same as a summary message that you see in Xcode for instances of Foundation classes.

如果我po objectName在控制台中输入,LLDB 会按预期显示良好的输出,但是 Xcode 和命令p objectName总是指示错误,那么使汇总字段工作的正确调试描述格式是什么?值得注意的是,“p”命令的输出与您在 Xcode 中看到的 Foundation 类实例的摘要消息相同。

Update:

更新:

As far as I can see from "WWDC 2012 session Debugging in Xcode", custom summaries can be implemented using Custom python script only. -(NSString*)descriptionor -(NSString*)debugDescriptionmethods are not connected anyhow to summary messages. I thought they are because I got an error displayed, but it seems it's a standard message for classes that do not have their own formatters.

据我从“WWDC 2012 session Debugging in Xcode”中可以看出,自定义摘要只能使用自定义 python 脚本来实现。-(NSString*)description-(NSString*)debugDescription方法无论如何都与摘要消息无关。我认为这是因为我显示了一个错误,但对于没有自己的格式化程序的类来说,这似乎是一条标准消息。

回答by Tommy

I would suggest at least:

我至少建议:

- (NSString*)description {
    return [NSString stringWithFormat:@"%@; x=%f, y=%f", [super description], _x, _y];
}

So that you're not manually replicating the NSObjectdefault and thereby blocking any non-default behaviour your superclass may have opted to include.

这样您就不会手动复制NSObject默认值,从而阻止您的超类可能选择包含的任何非默认行为。

Beyond that, "summary string parsing error" is an lldb error. It's being reported by the debugger only. Per its documentation, pois correct for Objective-C objects; pis for C or C++ objects. So you needn't heed that error — it's essentially just telling you that you used the wrong lldb command.

除此之外,“摘要字符串解析错误”是一个 lldb 错误。它仅由调试器报告。根据其文档po对于 Objective-C 对象是正确的;p用于 C 或 C++ 对象。所以你不需要注意那个错误——它本质上只是告诉你你使用了错误的 lldb 命令。

EDIT: for what it's worth, the method used by CFArrayis open sourceand looks like:

编辑:对于它的价值,使用的方法CFArray开源的,看起来像:

static CFStringRef __CFArrayCopyDescription(CFTypeRef cf) {
    CFArrayRef array = (CFArrayRef)cf;
    CFMutableStringRef result;
    const CFArrayCallBacks *cb;
    CFAllocatorRef allocator;
    CFIndex idx, cnt;
    cnt = __CFArrayGetCount(array);
    allocator = CFGetAllocator(array);
    result = CFStringCreateMutable(allocator, 0);
    switch (__CFArrayGetType(array)) {
    case __kCFArrayImmutable:
    CFStringAppendFormat(result, NULL, CFSTR("<CFArray %p [%p]>{type = immutable, count = %u, values = (%s"), cf, allocator, cnt, cnt ? "\n" : "");
    break;
    case __kCFArrayDeque:
    CFStringAppendFormat(result, NULL, CFSTR("<CFArray %p [%p]>{type = mutable-small, count = %u, values = (%s"), cf, allocator, cnt, cnt ? "\n" : "");
    break;
    }
    cb = __CFArrayGetCallBacks(array);
    for (idx = 0; idx < cnt; idx++) {
    CFStringRef desc = NULL;
    const void *val = __CFArrayGetBucketAtIndex(array, idx)->_item;
    if (NULL != cb->copyDescription) {
        desc = (CFStringRef)INVOKE_CALLBACK1(cb->copyDescription, val);
    }
    if (NULL != desc) {
        CFStringAppendFormat(result, NULL, CFSTR("\t%u : %@\n"), idx, desc);
        CFRelease(desc);
    } else {
        CFStringAppendFormat(result, NULL, CFSTR("\t%u : <%p>\n"), idx, val);
    }
    }
    CFStringAppend(result, CFSTR(")}"));
    return result;
}

As with the other comments above, I'm willing to gamble that the answer is: Xcode's debugger isn't smart in any sense and definitely isn't smart enough to use the correct pomeans of getting an Objective-C description; if your object is an uninflected Objective-C object then the debugger isn't going to be able to figure it out.

与上面的其他评论一样,我敢打赌答案是:Xcode 的调试器在任何意义上都不聪明,而且绝对不够聪明,无法使用正确的po方法来获取 Objective-C 描述;如果您的对象是未变形的 Objective-C 对象,则调试器将无法弄清楚。