打印在Swift 3扩展中不起作用 [英] print not working in Swift 3 extensions

查看:104
本文介绍了打印在Swift 3扩展中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift 3的新手,我尝试在Widget扩展中创建print("Test").

I'm new at Swift 3 and I try to make a print("Test") in a Widget extension.

我在 ViewController.swift 中尝试了相同的代码,并且工作正常.我不知道为什么它在那里工作,但是在 TodayViewController.swift 上却没有.我也无法访问objects.

I tried the same code in ViewController.swift and It works ok. I don't know why it works there but it doesn't on TodayViewController.swift. I can't access to objects too.

func loadData() {
    let query = PFQuery(className: "Noticias")
    query.whereKey("titulo", equalTo:"Es Navidad")
    query.findObjectsInBackground(block: { (objects : [PFObject]?, error: Error?) -> Void in
        if error == nil {
            // The find succeeded.
            print("Successfully retrieved \(objects!.count) scores.")

            // Do something with the found objects
            if let objects = objects {
                for object in objects {
                    print(object.objectId!)
                }
            }
        } else {
            // Log details of the failure
            print("bad day homie")
            print(error!)
        }
    })
}

我附上图片以清晰地看到它.如果我尝试在标记为工作"的文件上打印,则可以正常工作.但是,如果我在标有广告NO的文件上尝试使用它,则不会.

I attach I picture to see it clearly. If I try to print on the file marked as Work, it works. But if I try it on the file marked ad NO, it doesn't.

推荐答案

从扩展名中检索打印消息非常困难.问题是它是扩展!它未在您的应用程序中运行,因此不会到达您的控制台. 有时我发现您可以通过在调试区域顶部(在屏幕底部,未在屏幕快照中显示)但在其他区域的调试栏中切换调试过程来解决此问题.倍,这是行不通的.

It is extremely difficult to retrieve print messages from an extension. The problem is that it's an extension! It isn't running in your app, so it doesn't arrive at your console. Sometimes I find you can solve this problem by switching the debugged process in the Debug Bar at the top of the debug area (at the bottom of the screen, not shown in your screen shot), but at other times this doesn't work.

我将说明一种似乎非常可靠的可行技术.看一下这个屏幕截图:

I'll illustrate a possible technique that seems to be pretty reliable. Look at this screen shot:

扩展"是操作扩展.但是我包含的应用程序称为"bk2ch13 ...".那么,如何在动作扩展中的右边所示的断点处停顿呢?这就是我的工作.

"Expand" is an action extension. But my containing app is called "bk2ch13...". So how will I ever manage to pause at the breakpoint shown at the right, which is in the action extension? This is what I do.

  1. 首先,使用上面显示的屏幕,构建并运行我的包含应用程序.

  1. First, with the screen as shown above, I build and run my containing app.

然后,我将目标切换到动作扩展:

Then, I switch the target to the action extension:

  1. 现在,我再次构建并运行 .但是现在我正在尝试运行一个操作扩展,而您无法执行,因此Xcode询问我要运行哪个应用:
  1. Now I build and run again. But now I am trying to run an action extension, which you can't do, so Xcode asks me what app to run:

所以我选择"bk2ch13 ...".因此,现在我们再次运行我的主机应用程序,,但我们正在调试扩展程序.因此,我使用主机应用程序来执行扩展,当然,我们会在断点处暂停,并且print语句会进入控制台.

So I choose "bk2ch13...". So now we are running my host app again, but we are debugging the extension. So I use my host app to exercise the extension, and sure enough, we pause at the breakpoints and print statements arrive into the console.

请注意,在该屏幕快照中,调试栏如何清楚地表明我们正在与扩展程序而不是主机应用程序对话.

Note, in that screen shot, how the debug bar clearly shows that we are talking to the extension, not the host app.

这篇关于打印在Swift 3扩展中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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