Swift - 从字典数组中打印对象

时间:2016-03-14 16:15:00

标签: ios iphone swift

我想打印出#34; person"来自其中一本词典。我确定它很简单,但我找不到Swift 2.0的答案

var dic =
["person" : "Jane",
"country" : "US"]

var dic1 =
["person" : "Bill",
"country" : "UK"]

var dic2 =
["person" : "Jin",
 "country" : "NK"]

var bigArray = [dic, dic1, dic2]
var myAnswer = bigArray[1].valueForKey("person") as! String
print(myAnswer)

它会抛出错误,"类型的值' [String:String]'没有会员" valueForKey"。我在这里缺少什么?

4 个答案:

答案 0 :(得分:3)

你应该这样做:

var myAnswer = bigArray[1]["person"]!

答案 1 :(得分:1)

    let dic =
    ["person" : "Jane",
        "country" : "US"]

    let dic1 =
    ["person" : "Bill",
        "country" : "UK"]

    let dic2 =
    ["person" : "Jin",
        "country" : "NK"]

    let bigArray = [dic, dic1, dic2]
    let myAnswer = bigArray[1]["person"]
    print(myAnswer)

答案 2 :(得分:1)

尝试定义类型。

var dic1 : NSMutableDictionary =
["person" : "Bill",
"country" : "UK"]

答案 3 :(得分:0)

StorageFolder sf = KnownFolders.PicturesLibrary.GetFolderAsync("MyFolder");
StorageFile f = sf.GetFileAsync("MyFile.txt");
using(Stream s = await f.OpenForWriteAsync())
{
    using(StreamWriter sw = new StreamWriter(s, true))
    {
        sw.WriteLine("Others");
    }
}