Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: How to view the values of variables displayed in Xcode's watch window?

Tags:

xcode

swift

Simple question - how can you view the values of variables in Xcode's watch window when using Swift?

Here's an example, see how mdn has the value 2067134273 yet its not possible to view that in the watch window (ObjectiveC.NSObject doesn't expand to anything).

enter image description here

I thought this might be due to the fact that its an optional, however its not that straightforward because look at the display of onss which is displayed as "Optional NSString", its possible to view that and its an NSString?, while nss which is a non optional NSString doesn't have its value displayed.

Here's their declarations:

var nss:NSString = "NSString"
var ss = "Swift string"
var onss:NSString? = "Optional NSString"

So when debugging with Xcode how can the values of mdn and nss be viewed, and preferably be made to be displayed automatically without having to use the console?

like image 589
Gruntcakes Avatar asked Sep 10 '25 22:09

Gruntcakes


1 Answers

Xcode 6 beta 4 shows values of variables of NSString and NSString? types:

enter image description here

Nevertheless if you may need to see details for some other type even from some third-party framework that you cannot alter then you can implement debugQuickLookObject method and return custom description. In case of third-party lib you should wrap it in extension. For NSString it will be:

extension NSString {
    func debugQuickLookObject() -> AnyObject {
        return self
        // return "Here is debug value: \(self)"
    }
}

To preview it, just select the item in watch window and hit spacebar:

enter image description here

like image 114
Keenle Avatar answered Sep 12 '25 20:09

Keenle