Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check value existence by NSDate as key in dictionary

Tags:

ios

swift

I have a dictionary like this:

var dic = [NSDate: Int]()

it is used in my iOS to-do app to get the number of finished tasks of a particular date. I only care about the year, month and day sections in NSDate and also want to be able to get the number of tasks in a particular date using this dictionary, how can I do that? thanks.

like image 571
photosynthesis Avatar asked Jan 31 '16 07:01

photosynthesis


1 Answers

Instead of storing your date as NSDate in your dictionary you can save it as String so that comparison will be easier. Use following code to store it as a string

func dateFromString(date : NSDate) -> String {
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    return dateFormatter.stringFromDate(date)
}

You can pass NSDate() to above function and it will give you string containing only year, month and date. For retrieving your data from dictionary use following.

func dateFrom(year:Int, month:Int, day:Int) -> String {
    let components = NSDateComponents()
    components.year = year
    components.month = month
    components.day = day

    let gregorian = NSCalendar(identifier:NSCalendarIdentifierGregorian)
    let date = gregorian!.dateFromComponents(components)
    return dateFromString(date!)
}

You can pass year, month and date to above function and it will return corresponding date in string format. So your dictionary operations will look like

 dict[dateFromString(NSDate())] = 1 //for insertion or updation
 let numOfTasks = dict[dateFrom(2016, month: 1, day: 15)] //to get task for any particular day

EDIT

If you want to proceed with NSDate as key for your dictionary then you'll have to modify above code as follows. dateFrom will return date with year,month and date of your choice, and time will be some constant value. Time will be set to midnight in your current time zone if you don't set it.

func dateFrom(year:Int, month:Int, day:Int) -> NSDate {
    let components = NSDateComponents()
    components.year = year
    components.month = month
    components.day = day
    let gregorian = NSCalendar(identifier:NSCalendarIdentifierGregorian)
    let date = gregorian!.dateFromComponents(components)
    return date!
}

And for getting current date use following so that you store date object with current year, date, month and time to some constant value.

func getCurrentDate()->NSDate {
    let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components([.Day , .Month , .Year], fromDate: date)

    return dateFrom(components.year, month: components.month, day: components.day)
}

Usage will be as follows

dict[getCurrentDate()] = i //for insertion or updation
let numOfTasks = dict[dateFrom(2016, month: 1, day: 15)] //to get task for any particular day
like image 80
Vishnu gondlekar Avatar answered Oct 01 '22 07:10

Vishnu gondlekar