Using a variable property value for NSCalendarIdentifier return nil

152 views Asked by At

I'm trying to init an NSCalendar with a value that is variable like below:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:jam.calenderType];

But calendar always returns nil

I know that:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierIslamic];

And:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

both work fine but I want the value to be jam.calenderType which is an NSString that has the value of @"NSCalendarIdentifierIslamic" or @"NSCalendarIdentifierGregorian"

1

There are 1 answers

1
pteofil On BEST ANSWER

Your problem comes from the way you store the calendar identifier in your variable. When you wrote @"NSCalendarIdentifierIslamic" it simply means a string with that characters, and not the string that the NSCalendarIdentifierIslamic identifier points to.

So simply set your variable jam.calenderType = NSCalendarIdentifierIslamic; or jam.calenderType = NSCalendarIdentifierGregorian; and it would work fine.

Another easy solution would have been to make your calenderType as a BOOL or int and then based on it's value to chose NSCalendarIdentifierIslamic or NSCalendarIdentifierGregorian in your calendar init.