Swift:在 Xcode 7.3.1 中,print() 命令不会打印到控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38883080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: print() command not printing to console in Xcode 7.3.1
提问by ifiore
I'm currently in the process of making my first Swift project for iOs on Xcode. My current issue is that it seems that all print() commands I have in my code do not actually print text into my console window. I've looked this issue up in various places but cannot seem to find anyone else with the same problem, so I'm assuming I have some sort of syntax error or my understanding of swift/xcode is simply wrong. Here is the block of code I have:
我目前正在为 Xcode 上的 iO 制作我的第一个 Swift 项目。我当前的问题是,我的代码中的所有 print() 命令似乎实际上并没有将文本打印到我的控制台窗口中。我在不同的地方查过这个问题,但似乎找不到其他有同样问题的人,所以我假设我有某种语法错误,或者我对 swift/xcode 的理解是错误的。这是我拥有的代码块:
override func viewDidLoad() {
super.viewDidLoad()
// Configure the view.
let skView = view as! SKView
skView.multipleTouchEnabled = false
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("white",forKey: "petColor")
let bugcheck = defaults.stringForKey("petColor")
print(bugcheck)
print("hello yes this works")
// Create and configure the scene.
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
// Present the scene.
skView.presentScene(scene)
}
This is in the GameViewController.swift file that is made by default upon creation of a new project. The lines:
这是在创建新项目时默认创建的 GameViewController.swift 文件中。线路:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("white",forKey: "petColor")
let bugcheck = defaults.stringForKey("petColor")
print(bugcheck)
print("hello yes this works")
Is the part I am trying to get to work. I want to know that defaults are being correctly saved/loaded. When I run the app in simulator the view loads, and the scene is presented (I have some images pop up on screen), so I know that the code is atleast being run. However, nothing shows up on the console. What gives? Sorry if this question has been asked before but like I said I could not find a resource. Thank you for your time.
是我试图开始工作的部分。我想知道是否正确保存/加载了默认值。当我在模拟器中运行应用程序时,视图会加载并呈现场景(我在屏幕上弹出了一些图像),所以我知道代码至少正在运行。但是,控制台上没有任何显示。是什么赋予了?抱歉,如果之前有人问过这个问题,但就像我说的那样,我找不到资源。感谢您的时间。
回答by Edison
You need to set/add a breakpoint because sometimes methods in other files for example with recursive blocks or segues could be preempting the call of the print line. If you see your print statement after you have set a break point then you know something which is getting called is preempting the call of the print statement.
您需要设置/添加断点,因为有时其他文件中的方法(例如递归块或 segues)可能会抢占打印行的调用。如果您在设置断点后看到您的打印语句,那么您就知道正在调用的东西正在抢占打印语句的调用。
Adding, Disabling, and Deleting Breakpoints
Also, this may sound obvious so don't shoot me, but many people mistakenly disable the console view. shift+command+Cshows the console view.
此外,这听起来可能很明显,所以不要向我开枪,但很多人错误地禁用了控制台视图。shift+command+C显示控制台视图。
In addition, you can also use nslog
to print to device logs to see if it makes a difference. nslog
adds time-stamps etc. NSLog("Can anyone hear me?")
Then go to Xcode -> Window -> Devices and check the device logs. You can also check OS X Console Application for the same logs.
此外,您还可以使用nslog
打印到设备日志来查看是否有所不同。nslog
添加时间戳等。NSLog("Can anyone hear me?")
然后转到 Xcode -> Window -> Devices 并检查设备日志。您还可以检查 OS X 控制台应用程序以获取相同的日志。
回答by carlos.rios
That issue happened recently to me.
这个问题最近发生在我身上。
The problem was that I had connected my iPad as extended display using Duet app. Currently this connection is performed via Air Play and for some weird reason, logs in Xcode disappears.
问题是我使用 Duet 应用程序将 iPad 连接为扩展显示器。目前,此连接是通过 Air Play 执行的,出于某种奇怪的原因,Xcode 中的日志消失了。
Hope this help to someone else.
希望这对其他人有所帮助。