0

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

这是我的旧代码:

* 即将发布.h

@interface UpcomingRelease : NSObject

@property (nonatomic, strong) NSString *release_date;

- (NSString *) formattedDate;

@end

* 即将发布.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')。

4

1 回答 1

0

该错误已经表明NSDictionary没有名为formattedDate. 而现在你正在打电话_singleRelease.formattedDate,并且_singleRelease是一个NSDictionary.

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

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];
}
于 2013-10-28T15:37:53.793 回答