2017-03-07 27 views
-2

私はswiftで目的のCコードを変換しています。辞書スウィフトから辞書を取得

辞書に辞書があるサーバーからデータを取得しています。 キー値の文字列を取得していますが、辞書を取得できません。

データ例:

data =   { 
      caption = hello; 
      image =    { 
       a = "https://www.google.com/1024x1024"; 
       b = "https://www.google.com/640x640"; 
       c = "https://www.google.com/480x480"; 
       d = "https://www.google.com/"; 

      }; 
     }; 

私はキャプション

let dataDict = (mainDict[data] as? Dictionary<String,AnyObject>)! 

Obj.caption=String(dataDict["caption"]!) //getting hello 

Obj.imageDictionary = (dataDict["image"] as? Dictionary<String,String>)! //getting 0 key value pairs 

は、私はこの辞書を格納したい、画像辞書を取得する方法を提案してください

var imageDictionary = Dictionary<String, String>() 

としてimageDictionaryを初期化取得することができますimageDictionaryオブジェクトの

ご意見をいただければ幸いです! ありがとうございます!

+0

あなたは(単に '印刷用dataDictを[コンソールログを追加することができます"image"]) 'のようなものです。 –

+0

あなたのスペースは...芸術的です。 – Alexander

答えて

1

これを試してみてください:imageDictionary

let data : [String : Any] = ["caption" : "hello", 
          "image" :["a" : "https://www.google.com/1024x1024", 
         "b" : "https://www.google.com/640x640", 
         "c" : "https://www.google.com/480x480", 
         "d" : "https://www.google.com/", 
    ] 
] 

let caption = data["caption"] as! String 
let imageDictionary = data["image"] as! [String : String] 

を、私は取得しています:

["b": "https://www.google.com/640x640", "a": "https://www.google.com/1024x1024", "d": "https://www.google.com/", "c": "https://www.google.com/480x480"] 

スクリーンショット: enter image description here

+0

回答ありがとうございましたが、デバッグ時にコンソールチェックのimageDictionaryが0のキー値のペアを示しています – Gypsa

+0

私はコードのスクリーンショットを追加して出力しました。見てください。また、コンソールのデバッグでは、辞書のキー/値ペアが0になることがあります。オブジェクトを印刷してみてください。 – PGDev

+0

ありがとうございます – Gypsa