LLDB for Swift: Access computed property or perform function call in type summary Python script
The EvaluateExpression
function is available on the target as well as on frames. Try value.GetTarget().EvaluateExpression(...)
.
LLDB for Swift: Custom type summary for generic type
Use the --regex
/-x
flag to pattern match generic types in Swift, or template types in C++.
type summary add -s "This is a Foo" -x "^Baz\.Foo<.+,.+>$"
You can see many examples by running type summary list -l swift
. Here's how Dictionary
is shown:
^Swift\.Dictionary<.+,.+>$: (show children) (hide value) (skip references) Swift.Dictionary summary provider
How do I print the type or class of a variable in Swift?
Update September 2016
Swift 3.0: Use type(of:)
, e.g. type(of: someThing)
(since the dynamicType
keyword has been removed)
Update October 2015:
I updated the examples below to the new Swift 2.0 syntax (e.g. println
was replaced with print
, toString()
is now String()
).
From the Xcode 6.3 release notes:
@nschum points out in the comments that the Xcode 6.3 release notes show another way:
Type values now print as the full demangled type name when used with
println or string interpolation.
import Foundation
class PureSwiftClass { }
var myvar0 = NSString() // Objective-C class
var myvar1 = PureSwiftClass()
var myvar2 = 42
var myvar3 = "Hans"
print( "String(myvar0.dynamicType) -> \(myvar0.dynamicType)")
print( "String(myvar1.dynamicType) -> \(myvar1.dynamicType)")
print( "String(myvar2.dynamicType) -> \(myvar2.dynamicType)")
print( "String(myvar3.dynamicType) -> \(myvar3.dynamicType)")
print( "String(Int.self) -> \(Int.self)")
print( "String((Int?).self -> \((Int?).self)")
print( "String(NSString.self) -> \(NSString.self)")
print( "String(Array<String>.self) -> \(Array<String>.self)")
Which outputs:
String(myvar0.dynamicType) -> __NSCFConstantString
String(myvar1.dynamicType) -> PureSwiftClass
String(myvar2.dynamicType) -> Int
String(myvar3.dynamicType) -> String
String(Int.self) -> Int
String((Int?).self -> Optional<Int>
String(NSString.self) -> NSString
String(Array<String>.self) -> Array<String>
Update for Xcode 6.3:
You can use the _stdlib_getDemangledTypeName()
:
print( "TypeName0 = \(_stdlib_getDemangledTypeName(myvar0))")
print( "TypeName1 = \(_stdlib_getDemangledTypeName(myvar1))")
print( "TypeName2 = \(_stdlib_getDemangledTypeName(myvar2))")
print( "TypeName3 = \(_stdlib_getDemangledTypeName(myvar3))")
and get this as output:
TypeName0 = NSString
TypeName1 = __lldb_expr_26.PureSwiftClass
TypeName2 = Swift.Int
TypeName3 = Swift.String
Original answer:
Prior to Xcode 6.3 _stdlib_getTypeName
got the mangled type name of a variable. Ewan Swick's blog entry helps to decipher these strings:
e.g. _TtSi
stands for Swift's internal Int
type.
Mike Ash has a great blog entry covering the same topic.
Related Topics
How to Programmatically Change The Xdr Display Reference Mode (Aka Preset) on Macos
How to Allow a Generic Collection to Perform Under The Hood Conversion in Swift
Public Default Init in Protocol
Calling Consecutive Animations in Swift with Completion Handler
Swift - Setting Calayer Bounds or Frame Not Working
Comma Automatically Being Added to Textfield in Swift
Passing Data Between View Controllers (Swift)
Error Validating Cms Signature
Synchronous Request Using Alamofire
How to Create Viewcontrollers Without Storyboard and Set One as Delegate of The Other One
Implement a Crosshair Kind Behaviour in Realitykit
Dynamic Links Firebase Function Not Being Called at All Xcode 12
How to Change The Default Skscene That Displays on Startup
<Unknown>:0: Error: Opening Import File for Module 'swift': Not a Directory
Cannot Convert Value of Type 'Nsmutablearray' to Expected Argument Type '[Skaction]'