将字符串转换为字典数组

时间:2015-06-14 13:52:22

标签: arrays swift casting nsdate nsdictionary

我正在使用API​​做一些请求,它返回以下示例:

[{
id = 1004;
"planning_id" = 7;
"started_on" = "2015-05-14";}, 
{
id = 1380;
"planning_id" = 8;
"started_on" = "2015-05-16";}, 
{
id = 1382;
"planning_id" = 8;
"started_on" = "2015-05-18"; }]

如您所见,日期值作为字符串返回,现在被视为AnyObject ...

我现在正在尝试为数组的所有字典更改它,以便我可以将started_on值用作NSDate

这是我的代码:

        for i in 1..<myArray.count
    {

        let dateString = shiftsArray[i-1].objectForKey("started_on") as! String

        //format date
        var dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        var dateFromString = dateFormatter.dateFromString(dateString)

        shiftsArray[i-1].objectForKey("started_on") = dateFromString
    }

但是,由于最后一行出错,我无法运行代码:“无法分配类型'NSDate?'的值到“AnyObject?”类型的值。

问题是我不知道如何更改词典的'started_on'的值类型。

提前谢谢你,

2 个答案:

答案 0 :(得分:1)

我不知道你在问题的这个小片段之前是如何设置的,但这就是我的做法:

var arr:[[String:AnyObject]] = [["id":1, "planning_id":2, "started_on":"2015-05-13"], ["id":1, "planning_id":2, "started_on":"2015-05-14"], ["id":1, "planning_id":2, "started_on":"2015-05-10"]]

for (i, dict) in enumerate(arr) {
    let dateString = dict["started_on"] as! String

    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"

    let date = formatter.dateFromString(dateString)

    arr[i]["started_on"] = date
}

// You mentioned that you want to sort it based on date:
arr.sort { obj1, obj2 -> Bool in
    let date1 = obj1["started_on"] as! NSDate
    let date2 = obj2["started_on"] as! NSDate

    return date1.compare(date2) == NSComparisonResult.OrderedAscending
}

在:

var myArray = [
    [
        "id" : 1004,
        "planning_id" : 7,
        "started_on" : "2015-05-14"
    ],
    [
        "id" : 1380,
        "planning_id" : 8,
        "started_on" : "2015-05-16"
    ],
    [
        "id" : 1382,
        "planning_id" : 8,
        "started_on" : "2015-05-18"
    ]
]

后:

[{
    id = 1004;
    "planning_id" = 7;
    "started_on" = "2015-05-14 07:00:00 +0000";
}, {
    id = 1380;
    "planning_id" = 8;
    "started_on" = "2015-05-16 07:00:00 +0000";
}, {
    id = 1382;
    "planning_id" = 8;
    "started_on" = "2015-05-18 07:00:00 +0000";
}]

答案 1 :(得分:0)

如果您想这样做,那么您可以使用以下代码:

请参阅下面的编辑。

我个人希望将更改复制到新数组中,在这种情况下你可以使用map:

let newArray = myArray.map { (var obj) -> AnyObject in
    if let dateString = obj["started_on"] as? String {
        //format date
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let dateFromString = dateFormatter.dateFromString(dateString)
        obj["started_on"] = dateFromString
        return obj
    }
    return elem
}

修改

根据您的评论,以下是更改日期的示例:

    var myArray: [[String:NSObject]] = [
        [
            "id" : 1004,
            "planning_id" : 7,
            "started_on" : "2015-05-14"
        ],
        [
            "id" : 1380,
            "planning_id" : 8,
            "started_on" : "2015-05-16"
        ],
        [
            "id" : 1382,
            "planning_id" : 8,
            "started_on" : "2015-05-18"
        ]
    ]

for (index, var elem) in myArray.enumerate() {
    if let dateString = elem["started_on"] as? String {
        //format date
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let dateFromString = dateFormatter.dateFromString(dateString)
        elem["started_on"] = dateFromString
        myArray[index] = elem
    }
}

更改后访问数组:

// Returns value in an NSDate 
let nsdate = myArray[0]["started_on"]! // It's an optional!