내 코드 내 newDate 내가 나를 위해 작동하는 것 같다 이유구문 분석 할 때 NSDateFormatter가 nil을 반환하는 이유는 무엇입니까?
1
A
답변
1
잘 모릅니다이 시점에서 전무을 받고이
NSString *tempDate = [NSString stringWithString:tempReviewData.pubDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:@"HH:mm a"];
NSDate *newDate = [dateFormatter dateFromString:tempReviewData.pubDate];
비슷하지만 tempReviewData.pubDate
의 형식에 따라 달라집니다.
잘못된 형식 (예 : @"6:30 M"
)을 사용하면 null이 발생합니다.
이
노력하고 있습니다 : 출력NSString *tempDate = [NSString stringWithString:@"6:30 PM"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:@"HH:mm a"];
NSDate * newDate = [dateFormatter dateFromString:tempDate];
NSString * str = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"date: %@", newDate);
NSLog(@"str: %@", str);
:
2010-03-08 22:36:57.904 x[4340:903] date: 1970-01-01 12:30:00 +1000
2010-03-08 22:36:57.905 x[4340:903] str: 22:36 PM
1
NSDate *newDate = [dateFormatter dateFromString:tempReviewData.pubDate];
가있는 NSString 또는있는 NSDate를 반환 pubDate
합니까?
문자열을 반환하는 경우 해당 속성의 이름을 변경하여 명확하게 나타내야합니다.
날짜를 반환하면 (NSDate) 문자열이 아니므로 문자열로 구문 분석하려고하면 작동하지 않습니다. 게다가, 당신은 당신이 이미 데이트 객체를 가지고 있기 때문에이 포맷터 코드를 모두 잘라낼 수 있습니다.
0
NSDateFormatter는 매우 까다롭게 보입니다.
-(void)dateFormatterTests {
NSDateFormatter *formatter;
formatter = [[NSDateFormatter alloc] init];
#ifdef WORKS
[formatter setDateFormat:@"yyyy-MM-dd"];
#elif defined(ALSO_WORKS)
[formatter setDateFormat:@"yyyy MM dd"];
[formatter setLenient:YES];
#else // DOESN'T WORK
[formatter setDateFormat:@"yyyy MM dd"];
#endif
// Works per comments above
NSLog(@"dFS: %@", [formatter dateFromString:@"2010-01-13"]);
// Never works with any of the above formats
NSLog(@"dFS: %@", [formatter dateFromString:@"2010-01-13 22:00"]);
[formatter release]; formatter = nil;
}
관련 문제
- 1. NSDateFormatter가 nil을 반환하는 이유는 무엇입니까?
- 2. NSDateFormatter가 nil을 반환하는 이유는 무엇입니까?
- 3. NSDateFormatter가 때때로 a.m. 또는 p.m.를 반환하는 이유는 무엇입니까? yyyyMMddHHmmssSSS?
- 4. NSTemporaryDirectory가 nil을 반환하는 경우
- 5. 런타임에 클래스 만들기 - NSClassFromString과 objc_getclass가 nil을 반환하는 이유는 무엇입니까?
- 6. DSL에 구문 분석 도구가 필요한 이유는 무엇입니까?
- 7. jQuery를 사용하여 XML에서 구문 분석 할 때
- 8. xml을 구문 분석 할 때 xcode가 중단됩니까?
- 9. Apache를 구문 분석 할 때 Ansi C
- 10. NSDateFormatter로이 날짜 문자열을 올바르게 구문 분석 할 수없는 이유는 무엇입니까?
- 11. 이 SimpleDateFormat이이 날짜 문자열을 구문 분석 할 수없는 이유는 무엇입니까?
- 12. toString을 사용하여 javamail 첨부를 구문 분석 할 수없는 이유는 무엇입니까?
- 13. "super"가 NSObject 일 때 [super init]이 nil을 반환하는 이유는 무엇입니까?
- 14. XML을 PHP로 보낼 때 노드가 소문자 인 이유는 무엇입니까?하지만 PHP에서 구문 분석 할 때 대문자입니까?
- 15. xml 페이지를 반환하는 구문 분석 아약스 요청
- 16. metasexp로 구문 분석 할 때 오류를 표시하는 방법은 무엇입니까?
- 17. JPA로 간단한 쿼리를 구문 분석 할 때 구문 오류가 발생했습니다
- 18. AccessDataSource가 Access에서 쿼리 할 때 다른 결과를 반환하는 이유는 무엇입니까?
- 19. 서비스에서 호출 할 때 GetWindowThreadProcessId가 0을 반환하는 이유는 무엇입니까?
- 20. 구문 분석/구문 분석이란 무엇입니까?
- 21. 폴더가 일부 비스타 PC에있을 때 ShGetFolderPath가 nil을 반환하는 이유
- 22. Nokogiri의 xml 구문 분석 오류가
- 23. Netflix.com NewWatchInstantlyRSS 피드 항목 (피드 파서로 구문 분석 할 때)에 날짜가없는 이유는 무엇입니까? 다음에서
- 24. CSV 파일을 구문 분석 할 때 헤더 행을 무시하십시오.
- 25. 트래버스 할 때 표현식을 구문 분석/방문하는 다른 방법
- 26. Exception.fillInStackTrace가 Throwable을 반환하는 이유는 무엇입니까?
- 27. 구문 분석 JSON은
- 28. 구문 분석 XML 파일
- 29. Javascript에서 XML을 파싱 할 때 xsi : nil을 어떻게 확인합니까?
- 30. 가장 고생스러운 구문 분석
'tempDate' 변수와 그 안에 넣을 개체를 만드는 특별한 이유는 무엇입니까? 나중에 어느 쪽도 사용하지 마십시오. 당신은'pubDate' 메쏘드에서 반환 된 원래 날짜를 사용합니다. –