简体   繁体   中英

NSLocalizedString - Thread 1: EXEC_BAD_ACCESS with String.localizedStringWithFormat

I tried to make a notification with some localized variables. In the simulator, everything works without an error - even the notification shows up as it should, but if I test it on an iPhone, the app crashes.

    func getContent(fromName: String, andDue: String, andAmount: Double) -> UNMutableNotificationContent {
        let currency = self.appDelegate.settings.getShortCurrency() // standard währung aus dem System holen
        let content = UNMutableNotificationContent()


        content.subtitle = NSLocalizedString("REMINDERTITLE", comment: "Reminder: some payments are overdue...")


    // Error in this line:
    content.body = String.localizedStringWithFormat(NSLocalizedString("REMINDERTEXT", comment: "Hey, %@'s payment from %@ %@ is overdue since %@! Do you want to send him a reminder message?"), fromName, andAmount, currency, andDue)

        content.badge = 1
        content.sound = UNNotificationSound(named: "droppingCoin.wav")

        return content
    }

The error is:

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

in this line:

content.body = String.localizedStringWithFormat(NSLocalizedString("REMINDERTEXT", comment: "Hey, %@'s payment from %@ %@ is overdue since %@! Do you want to send him a reminder message?"), fromName, andAmount, currency, andDue)

The comment text is the actual localized text value defined in the .strings file.

Thanks in advance!

You're using the wrong format specifier for andAmount. andAmount is a double and not an object, so use %f instead of %@.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM