Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何重用NSString方法?_Ios_Objective C_Locale_Nsdateformatter - Fatal编程技术网

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;

}