-1

I have to locate these strings inside the if, which performs a countdown using the secondsRimanenti variable.

if secondiRimanenti > 1{
   self.statoRicerca = "Cerco sismometri disponibili... \(secondiRimanenti) secondi"
}
else{
   self.statoRicerca = "Cerco sismometri disponibili... \(secondiRimanenti) secondo"
}
self.tableView.reloadData()
self.timerStart()

The value of secondiRimanenti (seconds remaining) starts from 5 seconds down to 0 seconds This is the error:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x4)

I have tried these two ways:

let appSec = String(format: NSLocalizedString("Cerco sismometri disponibili... %@ secondi", comment: ""), secondiRimanenti)

and

let appSec = String.localizedStringWithFormat(NSLocalizedString("Cerco sismometri disponibili... %@ secondi", comment: ""), secondiRimanenti)

How can I solve it? Thanks!!

karlcess
  • 43
  • 6
  • Can you put a breakpoint just before this snippet? What is the value of `secondiRimanenti`? Btw if you want to localize plurals the recommended way is by using [stringsdict](https://medium.com/@vitaliikuznetsov/plurals-localization-using-stringsdict-in-ios-a910aab8c28c) – Alladinian Jul 04 '20 at 10:48
  • The value of secondiRimanenti (seconds remaining) starts from 5 seconds down to 0 seconds – karlcess Jul 04 '20 at 10:51
  • I understand that it's _supposed_ to hold a value between 0...5 but can you verify it actually _does_ via a breakpoint? – Alladinian Jul 04 '20 at 10:53
  • Essentially a duplicate of https://stackoverflow.com/questions/28620794/swift-nspredicate-throwing-exc-bad-accesscode-1-address-0x1-when-compounding. – Martin R Jul 04 '20 at 11:39

1 Answers1

1

You are using the wrong format specifier.

%@ is for objects, an Int (assuming that secondiRimanenti is Int) is %ld.

let appSec = String(format: NSLocalizedString("Cerco sismometri disponibili... %ld secondi", comment: ""), secondiRimanenti)
vadian
  • 274,689
  • 30
  • 353
  • 361