See runtime type or useful description of Any value?

Hi,


How do you see the runtime type and/or simple printed representation of an `Any` value in the debugger? For example, I have a `UIFont`. It's `descriptor.attributes` is a dictionary `[String: Any]`. I don't understand this print out of what's in the dictionary.


(lldb) p boldFont.fontDescriptor.fontAttributes[UIFontDescriptorTextStyleAttribute]!

(Any) $R13 = {

payload_data_0 = 0x00000001aacc8e38 @"CTFontEmphasizedUsage"

payload_data_1 = 0x0000000000000000

payload_data_2 = 0x0000000000000000

instance_type = 0x00000001017c8608

}

(lldb) p type(of:$R13)

(Any.Type) $R14 = 0x00000001017c8608


What does `payload_data_n` mean? Or the "@" sign? If that's a string as the value in the dictionary, is there some way to ask it the type and get the answer "String" or "Swift.String", and then be able to print the string, or float, or whatever.


(Xcode 8)


thanks,

Rob

Replies

Many types conform to CustomStringConvertible or CustomDebugStringConvertible, which means they have a "description" or "debugDescription" property, and you can use this easily in the debugger by using the "po" command instead of "p". (I think "po" is a synonym for "expr", but with a couple of explicit options.)


The @ sign just reflects the fact that the string is a NSString object. In Obj-C, a string literal without @ is a C-string.


Regarding the type, you could try (IIRC) "p type(ofValue: $R13)" to get the actual type. I may have the syntax wrong, since this type(of) thing is something that's a very recent change.


I strongly urge you to submit bug reports about this, for two reasons:


1. The debugger team is extremely responsive to feedback. I suspect they wish they more bug reports than they get, since developers often just throw up their hands when they hit a problem using the debugger.


2. The debugger currently has a tendency to show implementation-level details that I'm sure are wonderful for the Swift compiler writers, but don't help actual developers too much. Your case is a perfect example of this. You need the remind the team that developer-level usability matters, too.

What QuinceyMorris said but also…

What does

payload_data_n
mean?

These three payload words are an artefact of how protocol objects — and that includes the empty protocol, which is called

Any
— work in Swift. This was discussed in glorious detail in WWDC 2016 Session 416 Understanding Swift Performance. Highly recommended!

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"