2016-03-10 17 views
1

我想在應用程序打開時將loc-args數組的第二個元素從推送通知載荷設置爲loc-key翻譯。在didReceiveRemoteNotification方法中。在有效載荷中的LOC-args來如何在iOS中將loc-args與loc-key字符串合併推送通知

實施例是兩個元素的數組:

[ 
    "Apple", 
    "1 Infinite Loop Cupertino, CA 95014" 
] 

翻譯爲LOC-關鍵是:

Goto address: %[email protected]

如果推送消息到達時當應用程序在後臺工作正常。該消息顯示爲:

Goto address: 1 Infinite Loop Cupertino, CA 95014

但是,如果應用程序是在前臺我有我自己的didReceiveRemoteNotification方法來處理它。比如用:

let message = String(format: "Goto address: %[email protected]", 
        arguments: ["Apple", "1 Infinite Loop Cupertino, CA 95014"]) 

但這給出結果:Goto address: Apple而不是Goto address: 1 Infinite Loop Cupertino, CA 95014

誰能告訴我如何解決這個問題?

額外的信息:

如果我改變LOC-關鍵:Goto address: %[email protected] - %[email protected]文本將是:Goto address: 1 Infinite Loop Cupertino, CA 95014 - Apple

感謝。

+0

'字符串叉子(格式:...)'不允許*省略*位置參數。請參閱http://stackoverflow.com/questions/1063843/is-there-a-way-to-specify-argument-position-index-in-nsstring-stringwithformat中的註釋。 –

回答

1

直到更好的解決方案我做的for循環如下:

//grab the loc key 
var messageText = NSLocalizedString(locKey, comment: "Key for the alert message") 

//we need to merge the loc-args into the currentText 
if let locArgs:NSArray = alert["loc-args"] as? NSArray { 
    for (index, _) in locArgs.enumerate() { 
     messageText = messageText.stringByReplacingOccurrencesOfString("%\(index+1)[email protected]", withString: locArgs[index] as! String) 
    } 
} 
+0

接受你自己的答案,如果這解決了問題(它解決了我的:)) – Sulfkain

0

你只有一個「@」這樣的字符串只能得到「蘋果」。我認爲該位置將工作當兩者都得到了

0

在這裏,你必須從你的銀行代碼到對象 -

NSString *message = NSLocalizedString(locKey); 
NSString *format = @""; 
for (int i=0; i<[args count]; i++) { 
    format = [NSString stringWithFormat:@"%%%[email protected]", i+1]; 
    message = [message stringByReplacingOccurrencesOfString:format withString:args[i]]; 
} 
+0

爲什麼downvote?有些人需要Obj-C中的代碼,這對於人們來說可能是有用的 – Sulfkain