[swift-users] Customizing my custom type's appearance in the debugger

Tim Vermeulen tvermeulen at me.com
Sun Jun 26 19:03:12 CDT 2016


I already did that, sorry for not providing any code. Take this as an example:

public struct Wrapper<Element> {
    
    private var elements: [Element]
    
    public init<S: Sequence where S.Iterator.Element == Element>(_ sequence: S) {
        elements = [Element](sequence)
    }
    
}

extension Wrapper: Collection {
    
    public var startIndex: Int { return elements.startIndex }
    public var endIndex: Int { return elements.endIndex }
    
    public func index(after index: Int) -> Int {
        return index + 1
    }
    
    public subscript(position: Int) -> Element {
        return elements[position]
    }
    
}

extension Wrapper: CustomReflectable {
    
    public var customMirror: Mirror {
        return Mirror(self, unlabeledChildren: self, displayStyle: .collection)
    }
    
}

If I debug an instance of this Wrapper type, then Xcode’s Variables View will show

▿ wrapper
  ▿ elements = x values
    [0] = 0
    [1] = …

But the `elements` property is an implementation detail. What I would really want to see is this:

▿ wrapper = x values
  [0] = 0
  [1] = …

But I’m not sure if this is even possible. That’s basically why I’m asking this. Hopefully it’s clearer now :)

One last thing to note, the code `dump(wrapper)` will print

▿ 3 elements
  - 1
  - 2
  - 3

to the console, which is good. If I don’t implement CustomReflectable, then `dump(wrapper)` will show this:

▿ Wrapper<Swift.Int>
  ▿ elements: 3 elements
    - 1
    - 2
    - 3

So my CustomReflectable conformance is definitely doing something, but I would like to see the results in the variables view as well.

> On 27 Jun 2016, at 01:40, Dmitri Gribenko <gribozavr at gmail.com> wrote:
> 
> On Fri, Jun 24, 2016 at 3:53 PM, Tim Vermeulen via swift-users
> <swift-users at swift.org> wrote:
>> I’ve implemented a linked list. Now I’d like to be able to view the elements of a linked list in the debugger just like with an array. In the debugger, an array is represented like this:
>> 
>> [0] = the first element
>> [1] = the second element
>> etc
>> 
>> I wonder if I can do the same for my linked list. I already implemented CustomReflectable, so the code `dump(myLinkedList)` shows this in the console:
>> 
>> 3 elements
>>  - first element
>>  - second elements
>>  - third element
>> 
>> I thought this would also change the appearance of my linked list in the debugger, but unfortunately it’s unchanged. Is there a way to do what I’m trying to do?
> 
> Try setting "displayStyle: .collection" when you call the Mirror initializer.
> 
> Dmitri
> 
> -- 
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160627/1e9b36bd/attachment.html>


More information about the swift-users mailing list