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

标签 ios objective-c

我试图通过 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/

相关文章:

ios - 初始化核心数据 ManagedObjectContext

objective-c - 尚未创建 Firebase Analytics。 ld : warning: Some object files have incompatible Objective-C category definitions

iphone - 在 MonoTouch 中动态添加 socket

ios - 无法在 Swift 中使用 NSPredicate 查询日期

iphone - cell.titleLabel的字体设置应该在cellForRowAtIndexPath:方法中的什么位置?

objective-c - Objective-C 子类中的前向声明

ios - 推送 segue 的问题

iphone - 有没有办法以编程方式在iPhone中打开/关闭蓝牙?

ios - 访问嵌入在单元格中的文本字段

ios - 带有属性字符串的电子邮件编辑器