Ios 如何重用NSString方法?
我正在编写一个接受字符串并返回字符串的方法。我试着用另一种方法Ios 如何重用NSString方法?,ios,objective-c,locale,nsdateformatter,Ios,Objective C,Locale,Nsdateformatter,我正在编写一个接受字符串并返回字符串的方法。我试着用另一种方法 - (NSString*) formatDate:(NSString*) showStartDateTime { NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"UTC"]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:tz
- (NSString*) formatDate:(NSString*) showStartDateTime
{
NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"UTC"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:tz];
[dateFormatter setDateFormat:@"EEE MM/dd/yyyy"];
NSDate*dattt=[dateFormatter dateFromString:showStartDateTime ];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString*tempo2= [dateFormatter stringFromDate:dattt];
return tempo2;
}
这就是我使用它的方式。[自格式日期:@“Fri 08/08/2014”]使用它是否正确
NSString *hellostring=[self formatDate:@"Fri 08/08/2014"];
NSLog(@"%@",hellostring);
我知道它不起作用。感谢您的建议。我编写了一个命令行应用程序,但无法运行,因为在添加
dateFormatter.locale=[[NSLocale alloc]initWithLocaleIdentifier:@“en_US_POSIX”之前,dateFromString:
已经导致了nil代码>
现在它返回2014-08-08
请参见Define“不起作用”。我刚刚运行了它,我的控制台输出如预期的那样是“2014-08-08”。@danh我正在尝试看看是否可以重用此方法来简单地传递字符串并返回到正确的格式。@请定义“正确的格式”。也许你应该编辑这个问题,准确地说出你希望这个方法能做什么。@danh这个方法很明显是在试图将NSString日期从“EEE MM/dd/yyyy”转换为“yyyy-MM-dd”格式。它被日期格式化程序中的一些漏洞所咬,如果你试图解析一周中的某一天的值,它通常会失败。正如vikingosegundo所指出的,这个问题似乎以某种方式与相关联,但我更喜欢在尝试解析之前删除星期几信息来处理它。
- (NSString*) formatDate:(NSString*) showStartDateTime
{
NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"UTC"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:tz];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setDateFormat:@"EEE MM/dd/yyyy"];
NSDate*dattt=[dateFormatter dateFromString:showStartDateTime ];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString*tempo2= [dateFormatter stringFromDate:dattt];
return tempo2;
}