1

This function retrieves the array of item representation from disk, converts it to an array of TodoItem instances using an unnamed closure we pass to map, and sorts that array chronologically. Sorted doesn't work anymore, I get an error to switch to sort. But I can't seem to get it right. Whatever I do results in various errors.

func allItems() -> [TodoItem] {
        var todoDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ITEMS_KEY) ?? [:]
        let items = Array(todoDictionary.values)
        return items.map({TodoItem(deadline: $0["deadline"] as! NSDate, title: $0["title"] as! String, UUID: $0["UUID"] as! String!)}).sorted({(left: TodoItem, right:TodoItem) -> Bool in
            (left.deadline.compare(right.deadline) == .OrderedAscending)
        })
    }

1 Answer 1

1

Just replace sorted by sort, it looks like you've already done the rest of the translation (I can't check, not knowing how it was before):

func allItems() -> [TodoItem] {
    var todoDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ITEMS_KEY) ?? [:]
    let items = Array(todoDictionary.values)
    return items.map( {TodoItem(deadline: $0["deadline"] as! NSDate, title: $0["title"] as! String, UUID: $0["UUID"] as! String!)} ).sort( {(left: TodoItem, right:TodoItem) -> Bool in
        (left.deadline.compare(right.deadline) == .OrderedAscending)
    })
}

Reminder: sorted has become sort and the old sort is now sortInPlace.

Sign up to request clarification or add additional context in comments.

1 Comment

Be careful, this has changed again in Swift 3, where sort is the mutating method, and sorted is the one returning a new array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.