Alright. The problem we're having is that we have NSStrings filled with dates in the format of yyyyMMdd and what we want to do is to get the current weekday and name of month. The function dagOmvandlare converts the datestring to weekday and month. The function is then called on viewDidload to name all our button-titles from english to swedish months.
our current solution is looking like this:
-(NSString *)dagOmvandlare:(id) suprDatum{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"yyyyMMdd";
NSDate *date = [dateFormatter dateFromString:suprDatum];
NSString * monthString = [date descriptionWithCalendarFormat:@"%B"timeZone:nil
locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
if ([monthString isEqualToString:@"January"]) {
monthString = @"Januari";
}
else
if ([monthString isEqualToString:@"February"]) {
monthString = @"Februari";
}
else
if ([monthString isEqualToString:@"May"]) {
monthString = @"Maj";
}
else
if ([monthString isEqualToString:@"June"]) {
monthString = @"Juni";
}
else
if ([monthString isEqualToString:@"July"]) {
monthString = @"Juli";
}
else
if ([monthString isEqualToString:@"August"]) {
monthString = @"Augusti";
}
else
if ([monthString isEqualToString:@"October"]) {
monthString = @"Oktober";
}
else
if ([monthString isEqualToString:@"March"]) {
monthString = @"Mars";
}
return monthString;
}
- (void)viewDidLoad {
[super viewDidLoad];
[button3 setTitle:(NSString *)[self dagOmvandlare:self.datum1] forState:UIControlStateNormal];
[button2 setTitle:(NSString *)[self dagOmvandlare:self.datum2] forState:UIControlStateNormal];
[bmanad1 setTitle:(NSString *)[self dagOmvandlare:self.datum3] forState:UIControlStateNormal];
}
And what we've figured out so far is that the
NSString * monthString = [date descriptionWithCalendarFormat:@"%B"timeZone:nil
locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
is not allowed to be used by Apples guidelines regarding non-public api's. So the primary question is, what other way is there to get weekday and name of month out of our datestrings that contain dates with the format yyyyMMdd ?
this dayName will be available in the locale of user.
Hope this helps
This might be useful
Another SO question which might help you How to find weekday from today's date using NSDate?
As apple says, creating (and let ARC dispose) a formatter is expensive, so:
1) declare in your file:
2) use lazy approach:
if You have alla the stuff in one class, a static / let member would be nice. (we cannot use vars in extensions... :( globals seems bad but no way...)
Simple Swift 3 extensions: