2016-07-20 6 views
1

Apple Watch拡張機能の場合、AlertControllerを表示していて、「プリセット2」の代わりに「プリセットオプション(2)」のような表示が表示されます。私はオプションを取り除くにはどうすればよい文字列表示のオプションの取得

ここ
if let s = info["description"] 
{ 
    let action = WKAlertAction(title: "OK", style: WKAlertActionStyle.Default, handler: {() -> Void in 
     // 
    }) 

    //let arr = s.componentsSeparatedByString("_") 
    let arr = s.characters.split("_", maxSplit: Int.max, allowEmptySlices: false).map(String.init) 
    self.presentAlertControllerWithTitle("Preset \(arr[0])", message: "\n\(arr[1])", preferredStyle: WKAlertControllerStyle.Alert, actions: [action]) 
} 

がある(文字列がそれらを持っていたとは思いませんでした)どのように情報[「説明」]導き出される:配列の

func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) 
{ 
    if let info = message as? Dictionary<String,String>{    
     if let s = info["description"] 
     { 
      ... 
+0

ただ、今後の参考のために、任意の変数はオプションとすることができます。 – JAL

+0

Preset \(arr [0])のようにアンラップを強制してみてください! –

+3

@ VishalSonawane **いいえ。**本当に良い理由がある場合を除き、決してアンラップを強制しないでください。 – JAL

答えて

2

アンラップ内容や

if let str = arr[0] as? String { 
    let title = "Preset \(str)" 
} 

または

"Preset \(arr[0]) ?? \(someDefaultValue)" 
:nilを合体演算子を使用します

sもしあなたはそれが最初にアンラップしたいString?です:

if let s = info["description"] as? String { 
    // ... 
} 
+0

if str = arr [0]を?文字列 '文字列'から '文字列'への条件付きキャストは、常に警告に成功します。 –

+0

'arr'は文字列の配列で、添え字付き文字列の配列はオプションを返しません。だから私はOPが私たちが見ることができない別の問題を抱えていることを恐れている。私は 'info [" description "]'の内容を知りたいと思っています... – Moritz

+0

@EricDはい、配列はオプションのStringであると仮定しています。私たちは内容が何であるかを知る必要があります。 – JAL

関連する問題