I am trying to set the text of a UILabel to be equal to the name of the day of the week represented in an NSDateComponents. I am using the following code:
NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
dateComponents.weekday = 1; //Sunday
NSString * const weekdayNames[8] = {@"Array starts at 1", @"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday"};
UILabel *myLabel = [[[UILabel alloc] init] autorelease];
myLabel.text = weekdayNames[dateComponents.weekday]; //compiler error: Assignment of read-only variable 'prop.49'
I can make the code work in any of three ways:
- Make weekdayNames not be const
- Assign dateComponents.weekday to an intermediate int variable before using it as an array index
- Assign weekday[dateComponents.weekday] to an intermediate NSString * variable before calling setText:
But I want to know why my code, as originally written, fails.