2017-06-18 29 views
2

這裏的價值是一些代碼:抓鬥從字典<String, Any>斯威夫特卡倫特3

class MapViewController: UIViewController { 
    var dico:Dictionary<String, Any>? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let dest = "\(String(describing: self.dico?.index(forKey: "adresse"))) \(String(describing: self.dico?.index(forKey: "cp"))) \(String(describing: self.dico?.index(forKey: "ville")))" 
     print(dest)}} 

這就是「迪科」這是從賽格瑞來:

["cp": 1000, "id": 4, "loyer": , "ville": bruxelles, "nom": , "ref": garage2, "adresse": 50 rue de spa, "prenom": , "nouveau_loyer": ]

我想要得到它們各自的價值'dico'的元素作爲一個字符串重複使用後作爲一個長字符串(這是一個地址爲Geocoder) 任何人都知道?

由於我有這個錯誤以前的代碼:

Optional(Swift.Dictionary.Index(_value: Swift.DictionaryIndexRepresentation._native(Swift._NativeDictionaryIndex(offset: 13))))

回答

1

嘗試串插的這個組合,可選鏈接條件鑄造零合併運算??

let dest = "\(dico?["adresse"] as? String ?? "") \(dico?["cp"] as? String ?? "") \(dico?["ville"] as? String ?? "")" 

如果鍵的值"cp"真的是Int,那麼這樣做:

let dest = "\(dico?["adresse"] as? String ?? "") \(dico?["cp"] as? Int ?? 0) \(dico?["ville"] as? String ?? "")" 
+0

我不明白這些是什麼? 「」但它現在工作完美。非常感謝你 ! – BenoHiT

+0

* nil合併運算符*'??'展開一個可選項,如果它不爲零,並且它爲零,則它使用提供的默認值'「」'。無論哪種情況,您最終都會獲得一個非可選項,並且不會崩潰。 – vacawama

+0

aaah好吧,我明白了!謝謝你的解釋,讚賞 – BenoHiT