Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting day of week index to string objective c

Tags:

objective-c

currently, i am working on an app that uses Core Data. One of my managed objects has a property that keeps track of the day of week (Sunday - Saturday) as an integer (0-6). For the sake of sorting the objects by day as well as less overhead in saving, i definitely believe the best practice is to save the days as indexes and then convert to string during runtime. The question becomes the best practice to convert the index to its corresponding day as a string. ie. 0=>@"Sunday" and 6 => @"Saturday". I can obviously use NSCalendar and NSDate and NSDateComponents to achieve this. It just seems like a very roundabout way to go about it given the simplicity of the task. Naturally, a simple NSString array defined as such could do the trick:

NSString *dayOfWeek[7] = {@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday'"@"Saturday"};

But then i find myself constantly redefining this same variable over and over again. A global constant NSString could work. Another idea I had was creating a function that used this dayOfWeek array and then including it in the files that need it. What do you think. What's the best practice?

like image 891
ecbtln Avatar asked Jan 27 '26 05:01

ecbtln


1 Answers

How about one of the weekdaySymbols methods of NSDateFormatter?

like image 117
Hot Licks Avatar answered Jan 28 '26 21:01

Hot Licks