Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS crash on localized string with parameter

Tags:

ios

swift

Experiencing an odd issue here that only appears on some phones. I have a custom UIView that I init like so.

let passQuizToTutorAlert = SAAlertView(title: NSLocalizedString("quiz-title", comment: ""), message: String(format: NSLocalizedString("quiz-message-parameter", comment: ""), 4), textAlignment: .left, customView: headerImageView, alternativeLayout: true)

The line above crashes with this error: Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x4)

I played with the various methods in the init. It seems that it only crashes if I use a localized string with a parameter. For example the code here has no issues:

let passQuizToTutorAlert = SAAlertView(title: NSLocalizedString("quiz-title", comment: ""), message: NSLocalizedString("quiz-message", comment: ""), textAlignment: .left, customView: headerImageView, alternativeLayout: true)

The localized string it crashes on is defined like so:

"quiz-message-parameter" = "You have %d credits left";

What am I doing wrong here? Why does it only crash for a localized string with a parameter?

EDIT:

I did a little debugging. This crash is independent of the alert view. If I try:

let aString = String(format: "You have %d credits left", 4)
        print(astring)

It doesn't crash. But when it's mixed with localized string it crashes:

let aString = String(format: NSLocalizedString("quiz-message-parameter", comment: ""), 3)
like image 666
Kex Avatar asked Feb 21 '17 08:02

Kex


1 Answers

A format with a wrong placeholder for a variable causes an EXC_BAD_ACCESS.

In the case of NSLocalizedString(), you may want to check ALL versions of it for ALL the languages available.

It happened not long ago on a project of mine, for all the versions except one, the placeholder was %d, and in one it was %@, causing crash only when the phone was into the culprit language.

like image 144
Larme Avatar answered Sep 28 '22 05:09

Larme