0

I am nearing completion of my first SpriteKit project so I have been reading about the best way to hide all println() statements for release. The best way I found is as described here (Remove println() for release version iOS Swift).

So basically I have set my own custom println function at global scope like this

     func println(object: Any) {
Swift.println(object)
}

I haven't played around with the DEBUG Flag thing to do it automatically, for now I just comment the second line out. It works as expected, however I realised it is causing me some issues with my IAPs, specifically it crashes in this bit of code (and properly will at some other spots too).

     func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
    /* Payments */

    println("ADD PAYMENT")

    for transaction:AnyObject in transactions {
        var trans = transaction as! SKPaymentTransaction
        println(trans.error) //CRASH IS HERE

As soon as I remove this global println function it doesn't crash anymore. Any suggestions on why this is happening or if there is another way to stop all printlns. Obviously I could for now just do it manually, but it would be a bit of a pain since I like my printlns. I would appreciate any tips and tricks. Thank you

Community
  • 1
  • 1
crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • Can you pass the crash dump or a simple stacktrace of what you are getting? – John Difool Jun 07 '15 at 21:24
  • the console just gives me the BAD EXC , found nil while unwrapping. When you have this function all println statements in your code call it. – crashoverride777 Jun 07 '15 at 21:24
  • Its basically this, I am too new to coding to have any idea what this means "0 function signature specialization of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2) " Is this what you looking for ? – crashoverride777 Jun 07 '15 at 21:28
  • That error is when it expects a non-nil and it's getting one instead. Ex: 'var object: AnyObject? = nil; Swift.println(object!);'. I see you edited the code. Are you now passing a const String and it's still crashing? – John Difool Jun 07 '15 at 21:29
  • In the toolbar where you saw this error, just click on it and you will see a popup menu with the stacktrace. Select the one before so you can see where in your code you crash. Then look at the variables. Here is a link showing you what I am talking about: http://imgur.com/gjphQcU – John Difool Jun 07 '15 at 21:34
  • It must have something to do with the fact that the println it crashes is not a regular println such as println("Test"). Normally println are blue but using this custom function they now green. I believe this might be the issue, but don't know how to fix it. – crashoverride777 Jun 07 '15 at 21:35
  • I didn't edit my code. It crashes on the line println(trans.error). Sorry if Iam not specific enough – crashoverride777 Jun 07 '15 at 21:35
  • Wrap your code with a 'if let blah..." or test both trans and error because that's what the issue is. One of those variable is nil. That's what the green color tells you. – John Difool Jun 07 '15 at 21:39
  • you sure thats why the code is green? It is green everywhere because I use this costum println function. Could you give me an example of what you mean by if let. Thank you for your help – crashoverride777 Jun 07 '15 at 21:41
  • if let trans = transaction as? SKPaymentTransaction { Swift.println(trans.error) } // or in your case just a println() – John Difool Jun 07 '15 at 21:50
  • Just tried your suggestions, but it still crashes on this println(trans.error) line. Don't worry about it I will figure it or do it manually or maybe switch to NSLogs. Thank you very much for you help and patience, much appreciated. – crashoverride777 Jun 07 '15 at 21:55
  • I suggest you start using the debugger. I posted a reply with a test case. My gut feeling tells me that you are dereferencing a transaction with nil because passing nil to println would print nil not crash. Try using the debugger, it's not that difficult to see the variables and what is the real source of the problem. – John Difool Jun 07 '15 at 21:59

2 Answers2

0

Here is the code you can use reformatted nicely:

// Basic test case

    var transactions: [AnyObject] = [SKPaymentTransaction](count: 2, repeatedValue: SKPaymentTransaction() )
    transactions[1] = NSNull()

// Then the meat of the function

    for transaction in transactions {
        if let trans = transaction as? SKPaymentTransaction {
            println(trans.error) // call Swift.println
        }
    }
John Difool
  • 5,572
  • 5
  • 45
  • 80
0

This seems fixed now with Swift 2+.

crashoverride777
  • 10,581
  • 2
  • 32
  • 56