加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – Xcode中的NSObject描述和自定义摘要

发布时间:2020-12-14 19:57:59 所属栏目:百科 来源:网络整理
导读:我重写了对象的 – (NSString *)描述但是 Xcode总是显示错误:变量视图中的摘要字段中的摘要字符串解析错误. 我目前的实施如下: - (NSString*)description { return [NSString stringWithFormat:@"%@ %p x=%f,y=%f",self.class,self,_x,_y];} 如果我在控制
我重写了对象的 – (NSString *)描述但是 Xcode总是显示错误:变量视图中的摘要字段中的摘要字符串解析错误.

我目前的实施如下:

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

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

更新:

据我所知,“WWDC 2012会话在Xcode中调试”,自定义摘要只能使用自定义python脚本实现. – (NSString *)描述或 – (NSString *)debugDescription方法无论如何都不会连接到摘要消息.我认为它们是因为我显示了一个错误,但它似乎是没有自己的格式化程序的类的标准消息.

解决方法

我建议至少:

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

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

除此之外,“摘要字符串解析错误”是lldb错误.它仅由调试器报告.根据its documentation,po??对于Objective-C对象是正确的; p代表C或C对象.所以你不需要注意那个错误 – 它实际上只是告诉你你使用了错误的lldb命令.

编辑:对于它的价值,CFArray使用的方法是open source,看起来像:

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,CFSTR("<CFArray %p [%p]>{type = mutable-small,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,CFSTR("t%u : %@n"),idx,desc);
        CFRelease(desc);
    } else {
        CFStringAppendFormat(result,CFSTR("t%u : <%p>n"),val);
    }
    }
    CFStringAppend(result,CFSTR(")}"));
    return result;
}

与上面的其他评论一样,我愿意赌博答案是:Xcode的调试器在任何意义上都不聪明,并且绝对不够聪明,无法使用正确的方法来获取Objective-C描述;如果你的对象是一个未反射的Objective-C对象,那么调试器将无法弄清楚它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读