Author Topic: Error with Nil Coalescing Operator  (Read 6402 times)

sPecan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Error with Nil Coalescing Operator
« on: May 25, 2015, 09:49:20 AM »
In the last section of Chapter 4, The nil Coalescing Operator, I get an error in Playground.

Code: [Select]
var textColor: UIColor?
var color = textColor ?? UIColor.blackColor()

Code: [Select]
Playground execution failed: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x0).
* thread #1: tid = 0x12e378, 0x0000000000000000, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x0000000000000000
    frame #1: 0x0000000112c49078 $__lldb_expr105`__lldb_expr_105.(implicit closure #1) + 120 at Optionals.playground:20
    frame #2: 0x0000000112c4702e $__lldb_expr105`main + 4142 at Optionals.playground:20
    frame #3: 0x0000000107e32710 Optionals
    frame #4: 0x0000000107e365f1 Optionals`reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) + 17
    frame #5: 0x0000000107e351f1 Optionals`partial apply forwarder for reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) + 81
    frame #6: 0x0000000107e36620 Optionals`reabstraction thunk helper from @callee_owned (@in ()) -> (@out ()) to @callee_owned () -> (@unowned ()) + 32
    frame #7: 0x0000000107e36657 Optionals`reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @objc_block () -> (@unowned ()) + 39
    frame #8: 0x000000010847f41c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #9: 0x0000000108475165 CoreFoundation`__CFRunLoopDoBlocks + 341
    frame #10: 0x0000000108474923 CoreFoundation`__CFRunLoopRun + 851
    frame #11: 0x0000000108474366 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #12: 0x0000000108522661 CoreFoundation`CFRunLoopRun + 97
    frame #13: 0x0000000107e32dc2 Optionals`main + 1714
    frame #14: 0x000000010b121145 libdyld.dylib`start + 1
    frame #15: 0x000000010b121145 libdyld.dylib`start + 1

I suspect the error is with Playground, and isn't with the book example, because if I change the code, the operator works as expected.

Code: [Select]
var textColor: UIColor?
var color = textColor ?? 3

The funny thing is, when I use UIColor.blueColor() in the last step, it works.

Code: [Select]
var textColor: UIColor? = UIColor.blueColor()
var color = textColor ?? UIColor.blackColor()

So it isn't a problem with UIColor.

Please let me know if there's anything I can do about this. Or let me know why it's causing this error.

sPecan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error with Nil Coalescing Operator
« Reply #1 on: May 25, 2015, 11:35:16 AM »
The code works fine. It works fine alone. I checked it again and found that I only get that error when I add it to the code that was previously in Optionals.

Code: [Select]
//: Playground - noun: a place where people can play

import UIKit

var firstName: String = "Ryan"
var middleName: String? = nil
var firstAndMiddleNames: String
if let middle = middleName
{
firstAndMiddleNames = firstName + " " + middle
println("Middle name is not nil")
}
else
{
firstAndMiddleNames = firstName
println("Middle name is nil")
}

var textColor: UIColor?
var color = textColor ?? UIColor.blackColor()

I'd still like to know what's going on. Because each part of the code works fine by itself, when I comment out the other. So how is the first part interfering with the last?

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: Error with Nil Coalescing Operator
« Reply #2 on: May 25, 2015, 12:33:47 PM »
This is definitely a bug with playgrounds. If you comment out the line of code that sets the firstAndMiddleNames variable, the error goes away.

In this context, this line of code should not run because middleName is nil. In fact, if you copy the code out of the storyboard and paste it into a view controller and run it, it runs without a hitch. You're seeing the error because the storyboard is ignoring the "if let" statement and trying to execute the code:

Code: [Select]
var firstName: String = "Ryan"
var middleName: String? = nil
var firstAndMiddleNames: String
if let middle = middleName
{
    //firstAndMiddleNames = firstName + " " + middle
}
else
{
    firstAndMiddleNames = firstName
}

var textColor: UIColor?
var color = textColor ?? UIColor.blackColor()


All the best!
Kevin