Abbin Varghese Abbin Varghese - 4 months ago 9
Objective-C Question

Get name of the day in a week from the day number. (iOS)

I am trying to figure out a way to get the name of the day (ex : Monday, Tuesday..) from the day number(ex :1 for Monday, 2 for Tuesday..etc).

I have a

NSDictionary
structured like so.

{
close = {
day = 5;
time = "22:00";
dayName = "";
};
open = {
day = 5;
time = "9:00";
dayName = "";
};
}


I want to add the name of the day to this dictionary under the key "dayName"... where the value is the name of the day corresponding to the day number in the key "day".

Thank you :)

Answer

Let's start by getting the names of the days:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSArray *daySymbols = dateFormatter.standaloneWeekdaySymbols;

Now, this is an array of weekday names where at index 0 is Sunday, therefore you will have to convert your indices.

NSInteger dayIndex = 1; // 1 = Monday, ... 7 = Sunday
NSString *dayName = daySymbols[dayIndex % 7];
Comments