How can I Quick Look custom objects with Xcode 5 visual debugger?

早过忘川 提交于 2019-11-28 00:20:41

问题


Xcode 5 has a great new feature where you can hover over a variable name and get a visual representation of a UIColor, UIImage, or even UIBezierPath.

I vaguely remember a comment at WWDC where developers could either conform to some protocol or override some methods on any NSObject subclass in order to participate in this new debugging feature. I would love to add this to a bunch of my model objects to help me debug. Anyone know whether this is a real thing yet, or even if they hinted at it in a future release?

Unfortunately, Apple refers to this feature as "Quick Look" and since they have another technology called "Quick Look" my search results are very noisy and I can't find anything helpful.


回答1:


This is a new feature in Xcode 5.1, and the documentation on it can be found here. In a nutshell, you override -(id)debugQuickLookObject and return an OS type that already supports Quick Look, e.g. UIImage or NSAttributedString (full list of types in documentation):

- (id)debugQuickLookObject
{
    UIImage *image = [...];
    // Drawing code here
    return image;
}

For Swift:

There are a few options as of writing, none ideal:

  • Conform to CustomPlaygroundQuickLookable, but that only works in Playgrounds (and requires Xcode 7/Swift 2).
  • Use the same method as for Objective C. This requires your class to be marked @objc (or inherit a Objective-C class) as the caller relies on selectors.
  • Conform to Reflectable, but that requires you to provide a full custom MirrorType with a bunch of other properties along with the QuickLookObject (and doesn't even seem to work as of Xcode 7?)



回答2:


Now that 5.1 has been officially released I've released this new blog post on the matter.

To answer your question: Yes, this is indeed a feature available in the new release of XCode (v5.1) and can be used very easily by subclassing an object and returning whatever it is you want to see while debugging in a -(id)debugQuickLookObject method.



来源:https://stackoverflow.com/questions/21052666/how-can-i-quick-look-custom-objects-with-xcode-5-visual-debugger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!