Swift:如何查看 Xcode 监视窗口中显示的变量值? [英] Swift: How to view the values of variables displayed in Xcode's watch window?

查看:55
本文介绍了Swift:如何查看 Xcode 监视窗口中显示的变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题 - 使用 Swift 时如何在 Xcode 的观察窗口中查看变量的值?

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

这是一个例子,看看 mdn 的值是 2067134273 是如何在监视窗口中查看的(ObjectiveC.NSObject 不会扩展到任何东西).

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).

我认为这可能是因为它是可选的,但它并不那么简单,因为查看显示为可选 NSString"的 onss 的显示,可以查看它和它的 NSString?,而nss 是一个非可选的 NSString 没有显示它的值.

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.

这是他们的声明:

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

那么在用Xcode调试时,如何查看mdn和nss的值,最好是不用控制台就可以自动显示?

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?

推荐答案

Xcode 6 beta 4 显示 NSStringNSString? 类型变量的值:

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

然而,如果您可能需要查看其他类型的详细信息,即使是来自某些您无法更改的第三方框架,那么您可以实现 debugQuickLookObject 方法并返回自定义描述.如果是第三方库,您应该将其包装在扩展名中.对于 NSString,它将是:

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:

这篇关于Swift:如何查看 Xcode 监视窗口中显示的变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆