使用AFNetworking解析日期字符串(dateFormatter)

时间:2013-10-28 15:30:12

标签: ios json afnetworking afnetworking-2

我正在更改我的应用程序,因此可以通过AFNetworking解析我的JSON,当我尝试使用dateFormatter更改日期字符串时出现错误。我有一个名为“UpcomingRelease”的NSObject,一个名为“UpcomingReleasesViewController”的CollectionViewController和一个名为“ReleaseViewController”

的destinationViewController

这是我的旧代码:

* UpcomingRelease.h

@interface UpcomingRelease : NSObject

@property (nonatomic, strong) NSString *release_date;

- (NSString *) formattedDate;

@end

* UpcomingRelease.m

- (NSString *) formattedDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
    NSDate *newDate = [dateFormatter dateFromString:self.release_date];

    [dateFormatter setDateFormat:@"MMMM dd"];
    return [dateFormatter stringFromDate:newDate];
}

* ReleaseViewController.h (destinationViewController)

#import "UpcomingRelease.h"

@property (strong, nonatomic) NSDictionary *singleRelease;

@property (weak, nonatomic) IBOutlet UILabel *release_date;

* ReleaseViewController.m

@synthesize singleRelease = _singleRelease;
@synthesize release_date = _release_date;

- (void)viewDidLoad
{
    [super viewDidLoad];

    if([_singleRelease objectForKey:@"release_date"] != NULL)
    {
        self.release_date.text = [NSString stringWithFormat:@"%@", _singleRelease.formattedDate];
    }
    else
    {
        self.release_date.text = [NSString stringWithFormat:@"Releasing Soon"];
    }
}

我收到错误消息“在'NSDictionary'类型的对象上找不到”属性'formattedDate'。)

1 个答案:

答案 0 :(得分:0)

错误已经表明NSDictionary没有名为formattedDate的属性。现在,您正在呼叫_singleRelease.formattedDate_singleReleaseNSDictionary

将您的代码更改为以下内容:

if([_singleRelease objectForKey:@"release_date"] != NULL)
{
    NSString *rd = [_singleRelease objectForKey:@"release_date"]; // I assume that this is a string
    UpcomingRelease *upcoming = [[UpcomingRelease alloc] init];
    upcoming.release_date = rd;

    self.release_date.text = [NSString stringWithFormat:@"%@", upcoming.formattedDate];
}