OGeek|极客世界-中国程序员成长平台

标题: ios - 如何重用 NSString 方法? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:57
标题: ios - 如何重用 NSString 方法?

我正在编写一个接受字符串并返回字符串的方法。我正在尝试以另一种方法使用它。

- (NSString*) formatDateNSString*) 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;

}

这就是我使用它的方式。 [self formatDate"Fri 08/08/2014"] 是正确的使用方式吗?

NSString *hellostring=[self formatDate"Fri 08/08/2014"];
NSLog(@"%@",hellostring);

我知道它不起作用。任何建议表示赞赏。



Best Answer-推荐答案


我编写了一个命令行应用程序但无法运行它,因为 dateFromString: 已经导致 nil,直到我添加 dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier "en_US_POSIX"];

- (NSString*) formatDateNSString*) 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;

}

现在它返回 2014-08-08

Technical Q&A QA1480: NSDateFormatter and Internet Dates

关于ios - 如何重用 NSString 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24946656/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4