Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: print() command not printing to console in Xcode 7.3.1

Tags:

xcode

ios

swift

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:

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:

    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.

like image 833
ifiore Avatar asked Aug 10 '16 20:08

ifiore


People also ask

How do I display print in Xcode?

Press ⇧⌘Y or choose View > Debug Area > Show Debug Area to show the console output (or ⇧⌘C / Activate Console). Usually, this window will open automatically when your program produces output (this is controlled by the Behaviors section of Xcode's Preferences).

Where is the console in Swift?

The Console Output box will reappear at the top of the right-hand panel (the Assistant editor panel). Some errors may show in the Console box. Ignore. Now delete the /* and the Console box will stay put.


2 Answers

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.

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+C shows the console view.

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.

like image 157
Edison Avatar answered Nov 15 '22 07:11

Edison


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.

Hope this help to someone else.

like image 42
carlos.rios Avatar answered Nov 15 '22 05:11

carlos.rios