gpt4 book ai didi

ios - 为什么 NSDate 类型的变量不在初始化程序中初始化?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:51 25 4
gpt4 key购买 nike

我正在尝试通过 NSDateFormatter 使用 NSString 参数在初始化程序中初始化 NSDate 变量,结果我得到 null。有什么问题?
谢谢!
对不起我的英语不好。

@interface NewsEntity ()

@property (strong, nonatomic) NSString *newsSource;
@property (strong, nonatomic) NSString *newsTitle;
@property (strong, nonatomic) NSString *newsDescription;
@property (strong, nonatomic) NSURL *imageUrl;
@property (strong, nonatomic) NSDate *pubDate;

@end

@implementation NewsEntity

- (instancetype)initWithNewsSource:(NSString *)newsSource withNewsTitle:(NSString *)newsTitle withNewsDescription:(NSString *)newsDescription withImgeUrl:(NSURL *)imageUrl withPubDateString:(NSString *)pubDateString {
self = [super init];
if (self) {
_newsSource = newsSource;
_newsTitle = newsTitle;
_newsDescription = newsDescription;
_imageUrl = imageUrl;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
_pubDate = [dateFormatter dateFromString:pubDateString];

NSLog(@"Date: %@", _pubDate);
NSLog(@"Date string: %@", pubDateString);

}
return self;
}

@end
@interface NewsData ()

@property (strong, nonatomic) NSArray *dataSources;
@property (atomic) NSUInteger parsingFromDataSourcesComplete;
@property (strong, atomic) NSMutableArray *listOfNewsEntities;
@property (strong, nonatomic) NSMutableString *newsTitle;
@property (strong, nonatomic) NSMutableString *newsDescription;
@property (strong, nonatomic) NSString *imageUrlString;
@property (strong, nonatomic) NSMutableString *pubDate;
@property (strong, nonatomic) NSString *element;

@end

@implementation NewsData

- (instancetype)init {
self = [super init];
if (self) {
_dataSources = @[FIRST_SOURCE_URL, SECOND_SOURCE_URL];
}
return self;
}

- (void)getNewsData {
for (NSString *dataSource in self.dataSources) {
self.listOfNewsEntities = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:dataSource];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xmlParser setDelegate:self];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[xmlParser parse];
});
}
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
self.element = elementName;

if ([self.element isEqualToString:@"item"]) {
self.newsTitle = [[NSMutableString alloc] init];
self.newsDescription = [[NSMutableString alloc] init];
self.pubDate = [[NSMutableString alloc] init];
self.imageUrlString = nil;
}

if ([elementName isEqualToString:@"enclosure"]) {
self.imageUrlString = [attributeDict objectForKey:@"url"];
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([self.element isEqualToString:@"title"]) {
[self.newsTitle appendString:string];
}
else if ([self.element isEqualToString:@"description"]) {
[self.newsDescription appendString:string];
}
else if ([self.element isEqualToString:@"pubDate"]) {
[self.pubDate appendString:string];
}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {

NewsEntity *newsEntity = [[NewsEntity alloc] initWithNewsSource:nil withNewsTitle:self.newsTitle withNewsDescription:self.newsDescription withImgeUrl:[NSURL URLWithString:self.imageUrlString] withPubDateString:self.pubDate];

[self.listOfNewsEntities addObject:newsEntity];
}
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
dispatch_async(dispatch_get_main_queue(), ^{
[self prepareForSendingInfo];
});
}

- (void)prepareForSendingInfo {
self.parsingFromDataSourcesComplete++;
if (self.parsingFromDataSourcesComplete == self.dataSources.count) {
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_NEWS_INFO_IS_READY object:self.listOfNewsEntities];
}
}

@end

输出示例:

2015-06-04 18:21:34.384 TestAssignmentR[70762:12799790] Date: (null)  

2015-06-04 18:21:34.384 TestAssignmentR[70762:12799790] Date string: Thu, 04 Jun 2015 18:07:56 +0300

2015-06-04 18:21:34.385 TestAssignmentR[70762:12799790] Date: (null)

2015-06-04 18:21:34.385 TestAssignmentR[70762:12799790] Date string: Thu, 04 Jun 2015 18:05:56 +0300

2015-06-04 18:21:34.386 TestAssignmentR[70762:12799790] Date: (null)

2015-06-04 18:21:34.386 TestAssignmentR[70762:12799790] Date string: Thu, 04 Jun 2015 18:05:17 +0300

更新:

我已经按照您提供的方式更改了 dateFormatter,但没有帮助。
问题一定出在 NewsData 类中。
我也试过这个例子,它工作正常:

@interface TestDateFormatter ()

@property (strong, nonatomic) NSDate *date;

@end

@implementation TestDateFormatter

- (instancetype)initWithDateString:(NSString *)dateString {
self = [super init];
if (self) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
_date = [dateFormatter dateFromString:dateString];
NSLog(@"%@", dateString);
NSLog(@"%@", _date);
}
return self;
}

@end

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
NSString *dateString = @"Wed, 03 Jun 2015 13:17:00 +0300";
TestDateFormatter *test = [[TestDateFormatter alloc] initWithDateString:dateString];
}

@end

最佳答案

日期格式说明符不正确。您需要 ZZZ 作为时区偏移量:

@"EEE, dd MMM yyyy HH:mm:ss ZZZ"
^^^

此外,我认为在 init 方法中进行这种格式转换是相当不寻常的;传递一个已经初始化的 NSDate 对象会更加灵活,然后 init 方法不需要对用于表示字符串中日期的格式做出假设.这可以留给调用者。

关于ios - 为什么 NSDate 类型的变量不在初始化程序中初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30661095/

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