1

I'm developing an app with Swift and SpriteKit. I've converted my code from Swift 2.3 to Swift 3, but when I run the code I get the following error:

Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP.subcode=0x0)

The error show up on this line. I've created okLabel with SKLabelNode.

okLabel.fontName = (text: NSLocalizedString("dialogOKFont", comment: "")) as! String

On Localizable.strings(Base), I've defined "dialogOKFont" as follows:

"dialogOKFont" = "Optima-Bold";

I really appreciate if someone gives me an answer or hint to fix this error. Thank you in advance.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
vanagar
  • 3,511
  • 2
  • 10
  • 8
  • It looks like you're casting to a string outside of the parenthetical s for your font. Also, check out this extension http://stackoverflow.com/questions/25081757/whats-nslocalizedstring-equivalent-in-swift – TheValyreanGroup May 07 '17 at 13:45
  • 1
    ! means "please crash if what i'm looking for isn't there". – gnasher729 May 07 '17 at 14:25
  • 3
    Paulo Mattos' answer explains the problem. But do you really want to use NSLocalizedString to get a font name? – gnasher729 May 07 '17 at 14:28

1 Answers1

4

Try this instead:

okLabel.fontName = NSLocalizedString("dialogOKFont", comment: "")

Your original code had two related mistakes:

  • you wrapped the localized string in single element tuple: (text: ...)
  • and then you casted this tuple back to String, resulting in the EXC_BAD_INSTRUCTION crash

Using only NSLocalizedString(...) is enough to get the localized String you were after :)

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85