Localized String with Arguments for UserNotification Causing Service Error

When using NSString.localizedUserNotificationString(:_) with an argument, the system service responsible for user notifications fails.

[Connections] [REDACTED_BUNDLE_ID] Adding notification request failed with error: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.usernotifications.usernotificationservice" UserInfo={NSDebugDescription=connection to service named

If the arguments argument is nil, this problem does not occur.


Localizable.strings

"notification_one" = "Here's a number: %D";
"notification_two" = "Hello";

Swift

let notificationContentOne = UNMutableNotificationContent()
notificationContentOne.body = NSString.localizedUserNotificationString(forKey: "notification_one", arguments: [5])
UNUserNotificationCenter.current().add(notificationContentOne) // <-- Service error

let notificationContentTwo = UNMutableNotificationContent()
notificationContentTwo.body = NSString.localizedUserNotificationString(forKey: "notification_two", arguments: nil)
UNUserNotificationCenter.current().add(notificationContentTwo) // <-- No service error


Tested on device and simulator (Xcode 10b5).

Answered by chualan in 325394022

Solved.


It turns out the arguments provided must be a CVarArg, the API is confusing as it allows [Any] and will throw a confusing error in response to !CVarArg.

Accepted Answer

Solved.


It turns out the arguments provided must be a CVarArg, the API is confusing as it allows [Any] and will throw a confusing error in response to !CVarArg.

Localized String with Arguments for UserNotification Causing Service Error
 
 
Q