0

My first guess was that it would be debugDescription or description, but with NSURL, I'm getting something funny:

$ cd /tmp
$ xcrun swift
Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance.
  1> import Foundation
  2> NSURL(fileURLWithPath: "X")
$R0: NSURL = "X -- ile:///private/tmp/"

It's not debugDescription or description:

  3> NSURL(fileURLWithPath: "X").debugDescription
$R1: String = "X -- file:///private/tmp/"
  4> NSURL(fileURLWithPath: "X").description
$R2: String = "X -- file:///private/tmp/"

Clearly, also, if you simply define debugDescription and description (like class Foo here), it doesn't print those. (It uses them for print() and debugPrint().)

What is it printing in the repl by default, though?

Community
  • 1
  • 1
J. Cocoe
  • 881
  • 1
  • 10
  • 22

1 Answers1

2

The REPL is an extension of lldb. As such, for classes and structs, it is printing the type summary after evaluating. You can find more information about that here.

You can create a custom type summary for a class. For example, I can enter:

class MyClass: NSObject
{

}

let myClass = MyClass()

Into the REPL, which prints:

myClass: MyClass = {
  ObjectiveC.NSObject = {
    isa = __lldb_expr_45.MyClass
  }
}

I can then step out into lldb by typing : In lldb I can bind MyClass to a custom type summary by saying:

type summary add --summary-string "This is a MyClass" MyClass

I can drop back into the REPL by typing the command: repl

Now, when I say let myClass = MyClass(), I get:

myClass: MyClass = This is a MyClass
beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • Hmm, this is interesting and useful, but I'm even more confused as to how my path `"X"` is coming out as `"X -- ile:///private/tmp/"` now! – J. Cocoe Aug 09 '16 at 19:17
  • I'm guessing it's a typo in the type summary of `NSURL`, but I don't have any specific insight into how it got there. – beyowulf Aug 09 '16 at 20:32