gpt4 book ai didi

objective-c - 将 NSString 转换为 NSDate 的问题

转载 作者:行者123 更新时间:2023-11-28 20:31:39 24 4
gpt4 key购买 nike

我在将 NSString 转换为 NSDate 时遇到了一些问题。

这是我的代码:

NSLog(@"Unformatted date: %@", [d objectForKey:@"pubDate"]);

// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}
[self setPublicationDate:[dateFormatter dateFromString:[d objectForKey:@"pubDate"]]];

NSLog(@"Date object: %@", [self publicationDate]);
NSLog(@"Date object description: %@", [[self publicationDate] description]);

当它运行时,它会记录这些消息(它会运行几次,我只包含了两组日志消息):

2012-08-14 12:47:28.446 (App name removed)[9009:707] Unformatted date: Mon, 02 Apr 2012
2012-08-14 12:47:28.454 (App name removed)[9009:707] Date object: (null)
2012-08-14 12:47:28.460 (App name removed)[9009:707] Date object description: (null)
2012-08-14 12:47:28.467 (App name removed)[9009:707] Unformatted date: Sun, 25 Mar 2012
2012-08-14 12:47:28.474 (App name removed)[9009:707] Date object: (null)
2012-08-14 12:47:28.480 (App name removed)[9009:707] Date object description: (null)

我正在运行 iOS 5.1.1 的 iPad touch 上测试这段代码

我能够更改接收到的日期格式,它目前是从 PHP 日期函数发送的,"D, d M Y" 被用作格式字符串。

谁能看出我哪里出错了?感谢您的帮助。

更新 1:我已经切换并在模拟器上而不是在设备上运行它,并且由于某种原因它起作用了。关于为什么它不能在设备上工作的任何建议?模拟器运行iOS 5.1,设备运行iOS 5.1.1

更新 2:有些人要求提供更多信息,publicationDate 是一个综合属性。

在.h文件中:

@property (nonatomic, strong) NSDate *publicationDate;

在 .m 文件中:

@synthesize publicationDate;

我进一步分解了代码:

NSLog(@"Unformatted date: %@", [d objectForKey:@"pubDate"]);
NSLog(@"Unformatted date class type: %@", [[d objectForKey:@"pubDate"] class]);

// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}
NSString *dateString = [d objectForKey:@"pubDate"];
NSLog(@"dateString: %@", dateString);

NSDate *formattedDate = [dateFormatter dateFromString:dateString];
NSLog(@"formattedDate %@", formattedDate);
[self setPublicationDate:formattedDate];

NSLog(@"Date object: %@", [self publicationDate]);
NSLog(@"Date object description: %@", [[self publicationDate] description]);

现在记录:

2012-08-14 13:45:35.335 (app name removed)[9167:707] Unformatted date: Sun, 25 Mar 2012
2012-08-14 13:45:35.342 (app name removed)[9167:707] Unformatted date class type: __NSCFString
2012-08-14 13:45:35.347 (app name removed)[9167:707] dateString: Sun, 25 Mar 2012
2012-08-14 13:45:35.353 (app name removed)[9167:707] formattedDate (null)
2012-08-14 13:45:35.359 (app name removed)[9167:707] Date object: (null)
2012-08-14 13:45:35.365 (app name removed)[9167:707] Date object description: (null)

更新 3:成功!通过像这样更改日期格式化程序:

// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}

现在可以了。我不想提及被测试的设备设置为荷兰设置(因为这是一家荷兰开发公司)。抱歉,我从未想过这可能会影响此处代码的工作方式。

最佳答案

你确定你从下面的代码中得到了一个字符串对象吗?

[d objectForKey:@"pubDate"]

尝试记录从 d 返回的对象的类。否则代码似乎没问题。

编辑:检查设备中的区域设置:转到设备中的“设置”、“通用”、“国际”。如果区域格式未设置为英语,则必须在 dateformatter 中使用 setLocale 方法查看此问题的已接受答案以获取更多信息:How do you interpret dates with NSDateFormatter?

关于objective-c - 将 NSString 转换为 NSDate 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11950870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com