2013-10-28 22 views
0

我正在更改我的應用程序,以便它可以通過AFNetworking解析我的JSON,並且當我嘗試使用dateFormatter更改日期字符串時出現錯誤。我有一個NSObject的叫「UpcomingRelease」,一個叫CollectionViewController 「UpcomingReleasesViewController」和destinationViewController稱爲「ReleaseViewController」使用AFNetworking解析日期字符串(dateFormatter)

這是我的舊代碼:

* 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"]; 
    } 
} 

我得到一個錯誤說「財產 'formattedDate' 沒有找到對象類型'NSDictionary')。

+0

好像與物業formattedDate對象已被釋放,當您嘗試訪問它。我認爲probelm不是日期格式化程序,但你如何處理hs這個屬性的對象 – Manu

回答

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]; 
} 
+0

UpcomingRelease是一個NSObject,singleRelease是我的destinationViewController(ReleaseViewController)的一個NSDictionary – ChrisBedoya

+0

你是什麼原因使用UpcomingRelease? – tilo

+0

我作爲基地使用的教程有這樣的代碼設置。但我猜測是要存儲NSStrings並更改日期以便可讀。 – ChrisBedoya