2017-07-14 24 views
0

Localizable.stringsから返された値を持つキーを持つプッシュ通知をデバイスに送信しました。引数の数は可変ですが、時には1つだけ必要になることもありますが、必要な場合もあります。未知数の引数を持つ文字列をローカライズ

通常、私はこれを使用する文字列をローカライズする:

String.localizedStringWithFormat(NSLocalizedString(notification.localisedKey, comment: "a comment"), notification.localisedArguments) 

notification.localisedArgumentsが単一の値である場合は、これはのみ動作します。配列の場合、クラッシュします。どのように私はiOSの9と下の等価を行うことができます

NSString.localizedUserNotificationString(forKey: notification.localisedKey, arguments: notification.localisedArguments) 

iOSの10でこの問題を回避するには、次のコードを使用しているのですか?

+0

関連:https://stackoverflow.com/questions/42457503/how-to-properly-use-varargs-for-localizing-strings。 –

+0

@MartinRそのコードはそれを解決しました!ありがとう! – Tometoyou

答えて

0

は(未テスト)これを試してみてください:

let localizedString = NSLocalizedString(notification.localisedKey, comment: "a comment") 
let result = withVaList(notification.localisedArguments) { 
    String.localizedStringWithFormat(localizedString, $0) 
} 
+0

これは、 "引数タイプ 'CVaListPointer'が$ 0 – Tometoyou

+0

のための予想されるタイプ 'CVarArg'に適合していないというエラーを示しています@Tometoyou Hmmm、これを代わりに試してくださいhttps://developer.apple.com/documentation/swift/1539624-getvalist – Alexander

+0

それは同じエラーを生成します:( – Tometoyou

関連する問題