2013-08-29 6 views
3

SO를 통해 검색했지만 this question을 제외하고는 솔루션을 찾지 못했습니다. 나는 주 번호의 int 올해의 int을 받아 들일 것이며, 그 달의 이름으로 NSString을 반환하는 방법 만드는 방법에 대해 생각하고 있어요 :특정 연도의 주 번호를 월 이름으로 변환

- (NSString *)getMonthNameFromNumber:(int)weekNumber andYear:(int)year

을하지만 그럴 수 없어 이 문제에 접근하는 방법을 찾으십시오. 누군가가 조언을 도울 수 있다면 기쁠 것입니다. 이 장치 환경 설정에 설정되어있는 현재의 달력에 의존한다는이 같은

+2

NSDateComponents 유용 할 수 있습니다. – Larme

+0

'setDateFormat' 대신 NSDateFormatter'monthSymbols'를 사용하십시오. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/monthSymbols –

답변

4

뭔가

- (NSString *)monthNameForWeek:(NSUInteger)week inYear:(NSInteger)year { 
    NSDateComponents * dateComponents = [NSDateComponents new]; 
    dateComponents.year = year; 
    dateComponents.weekOfYear = week; 
    dateComponents.weekday = 1; // 1 indicates the first day of the week, which depends on the calendar 
    NSDate * date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"MMMM"]; 
    return [formatter stringFromDate:date]; 
} 

참고 할 것입니다.

사용자의 요구에 맞지 않는 경우 NSCalendar 인스턴스를 제공하고 currentCalendar 대신 날짜를 검색하는 데 사용할 수 있습니다. 이렇게하면 일주일 중 첫 번째 날과 같은 일을 구성 할 수 있습니다. documentationNSCalendar이면 읽을 가치가 있습니다. 사용자 정의 달력을 사용하는 일반적인 경우라면 당신은 반환하지 않는

, 그냥, 당신이 방법 이름을 get을 피해야한다,

- (NSString *)monthNameForWeek:(NSUInteger)week inYear:(NSInteger)year { 
    [self monthNameForWeek:week inYear:year calendar:[NSCalendar currentCalendar]]; 
} 

- (NSString *)monthNameForWeek:(NSUInteger)week inYear:(NSInteger)year calendar:(NSCalendar *)calendar { 
    NSDateComponents * dateComponents = [NSDateComponents new]; 
    dateComponents.year = year; 
    dateComponents.weekOfYear = week; 
    dateComponents.weekday = 1; // 1 indicates the first day of the week, which depends on the calendar 
    NSDate * date = [calendar dateFromComponents:dateComponents]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"MMMM"]; 
    return [formatter stringFromDate:date]; 
} 
관련이없는 보조 노트로

처럼 뭔가 구현을 변경 간접적으로 가치.

+0

... 돌아올 것입니다. 그주의 최초의 날이 속하는 달의 이름. 다른 날짜 구성 요소를 사용하여 6 일을 진행하고 몇 주에 걸쳐 몇 주를 염려한다면 마지막 주를 확인할 수 있습니다. 또한 다른 지역에서 약간 다른 답변을 준비하십시오. 월요일은 세계 대부분에서 일주일의 시작이지만 미국에서는 일요일입니다. 나는 애플이 주 N이 시작될 때 당신이 이야기 할 때 그것을 고려해야한다고 생각한다. – Tommy

+0

이것은 작동하지 않습니다. 다시 null이됩니다. – WDUK

+0

죄송합니다. 잘못된 방식으로 날짜가 표시되었습니다. 고정 –

2

날짜와 관련이있는 경우 일정 관리가 필요합니다. 귀하의 질문은 태양력을 가정합니다,하지만 난 당신이 당신의 메소드 선언을 변경 제안이에서

- (NSString*)monthNameFromWeek:(NSInteger)week year:(NSInteger)year calendar:(NSCalendar*)cal; 

, 또한 우리가 이야기하고있는 일의 모호성이있다. 예를 들어 (확인되지 ​​않았습니다.) 2015 년 4 월에는 1 월과 2 월이 모두 포함될 수 있습니다. 어느 것이 옳은가요? 이 예에서는 일요일 (영국의 그레고리오 달력)을 나타내는 1의 평일을 사용하며이 날짜가 오는 달을 사용합니다.

따라서, 코드는 다음과 같습니다

// Set up our date components 
NSDateComponents* comp = [[NSDateComponents alloc] init]; 
comp.year = year; 
comp.weekOfYear = week; 
comp.weekday = 1; 

// Construct a date from components made, using the calendar 
NSDate* date = [cal dateFromComponents:comp]; 

// Create the month string 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"MMMM"]; 
return [dateFormatter stringFromDate:date]; 
관련 문제