Converting String into NSDate in array of dictionaries

519 views Asked by At

I am doing some requests with an API that returns me the following sample :

[{
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"; }]

As you can see, the date value is returned as a string which is considered as AnyObject for now...

I am now trying to change it for all the dictionaries of the array so I can use the started_on value as a NSDate

Here is my code :

        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
    }

However, I cannot run the code since I have an error for the last line : "Cannot assign a value of type 'NSDate?' to a value of type "AnyObject?".

The problem is that I don't know how to change the value type of the 'started_on' of the dictionnaries.

Thank you in advance,

2

There are 2 answers

8
Arbitur On BEST ANSWER

I dont know how you set up things before this little snippet in you question but this is how I did:

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
}

Before:

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"
    ]
]

After:

[{
    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";
}]
10
John Difool On

If you would like to do it in place then you can use this code:

See my EDIT below.

I would personally want to copy the changes into a new array and you can use map in that case:

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
}

EDIT

Per your comment, here is an example of changing the date in place:

    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
    }
}

Accessing the array after changes:

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