2016-02-02 86 views
0

我遇到了一些麻煩,我從來沒有遇到過使用NSUserDefaults。NSUserDefaults沒有保存完整對象

我有一個自定義類對象,我用它來解析API的響應。該類符合NSCoding並實現了initWithCoder和encodeWithCoder。

在SiteGroup.h文件:

@interface SiteGroup : NSObject <NSCoding> 
@property (nonatomic, assign) int SiteGroupID; 
@property (nonatomic, assign) int StatusID; 
@property (nonatomic, copy) NSString *StatusDesc; 
@property (nonatomic, copy) NSString *SiteGroupName; 
@property (nonatomic, copy) NSString *City; 
@property (nonatomic, copy) NSString *State; 
@property (nonatomic) Country *SiteGroupCountry; 
@property (nonatomic, copy) NSString *LocalConference; 
@property (nonatomic, copy) NSString *Address; 
@property (nonatomic, copy) NSString *ClosestAirport; 
@property (nonatomic, copy) NSString *Coordinates; 
@property (nonatomic, copy) NSString *Latitude; 
@property (nonatomic, copy) NSString *Longitude; 
@property (nonatomic, copy) NSString *Altitude; 
@property (nonatomic, copy) NSString *Accuracy; 
@property (nonatomic, copy) NSArray *SiteGroupEvents; 
@property (nonatomic, copy) NSArray *Sites; 

而在SiteGroup.m文件:

- (id)initWithCoder:(NSCoder *)decoder { 
self = [super init]; 
if (!self) { 
    return nil; 
} 
self.SiteGroupID = [decoder decodeIntForKey:@"SiteGroupID"]; 
self.SiteGroupName = [decoder decodeObjectForKey:@"SiteGroupName"]; 
self.City = [decoder decodeObjectForKey:@"City"]; 
self.State = [decoder decodeObjectForKey:@"State"]; 
self.SiteGroupCountry = [decoder decodeObjectForKey:@"SiteGroupCountry"]; 
self.LocalConference = [decoder decodeObjectForKey:@"LocalConference"]; 
self.Address = [decoder decodeObjectForKey:@"Address"]; 
self.ClosestAirport = [decoder decodeObjectForKey:@"ClosestAirport"]; 
self.Coordinates = [decoder decodeObjectForKey:@"Coordinates"]; 
self.Latitude = [decoder decodeObjectForKey:@"Latitude"]; 
self.Longitude = [decoder decodeObjectForKey:@"Longitude"]; 
self.Altitude = [decoder decodeObjectForKey:@"Altitude"]; 
self.Accuracy = [decoder decodeObjectForKey:@"Accuracy"]; 
self.SiteGroupEvents = [decoder decodeObjectForKey:@"SiteGroupEvents"]; 
self.Sites = [decoder decodeObjectForKey:@"Sites"]; 
self.StatusID = [decoder decodeIntForKey:@"StatusID"]; 
self.StatusDesc = [decoder decodeObjectForKey:@"StatusDesc"]; 
return self; 
} 

- (void)encodeWithCoder:(NSCoder *)encoder { 
[encoder encodeInt:self.SiteGroupID forKey:@"SiteGroupID"]; 
[encoder encodeObject:self.SiteGroupName forKey:@"SiteGroupName"]; 
[encoder encodeObject:self.City forKey:@"City"]; 
[encoder encodeObject:self.State forKey:@"State"]; 
[encoder encodeObject:self.SiteGroupCountry forKey:@"SiteGroupCountry"]; 
[encoder encodeObject:self.LocalConference forKey:@"LocalConference"]; 
[encoder encodeObject:self.Address forKey:@"Address"]; 
[encoder encodeObject:self.ClosestAirport forKey:@"ClosestAirport"]; 
[encoder encodeObject:self.Coordinates forKey:@"Coordinates"]; 
[encoder encodeObject:self.Altitude forKey:@"Altitude"]; 
[encoder encodeObject:self.Accuracy forKey:@"Accuracy"]; 
[encoder encodeObject:self.SiteGroupEvents forKey:@"SiteGroupEvents"]; 
[encoder encodeObject:self.Sites forKey:@"Sites"]; 
[encoder encodeInt:self.StatusID forKey:@"statusID"]; 
[encoder encodeObject:self.StatusDesc forKey:@"statusDesc"]; 
} 

一旦我做了解析它,它被添加到一個的NSMutableArray。然後檢查該數組的大小是否大於0,如果是,則將其存儲到NSUserDefaults中。

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[NSArray arrayWithArray:tempSiteGroupList]]; 
    [userDefaults setObject:data forKey:@"SiteGroupArray"]; 
    [userDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:tempSiteGroup] forKey:@"SiteGroup"]; 

    [userDefaults synchronize]; 

的陣列也得到保存到NSUserDefaults的,但奇怪的是,我的自定義類的不是所有屬性得到保存,有的做,有的則沒有。 下面是我用帶回陣列的NSUserDefaults的代碼:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
[userDefaults synchronize]; 
NSData *data = [userDefaults objectForKey:@"SiteGroupArray"]; 
NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
return myArray; 

的StatusID,StatusDesc,緯度,經度和SiteGroupID沒有被保存,即使一些其他屬性都很好。

感謝您的幫助,我從未遇到過這種情況,也許我一直在做錯事。

回答

1

StatusIDStatusDesc有不同的密鑰(@"StatusID"爲編解碼@"statusID",同樣是有效的StatusDesc)的解碼和編碼,在與CRD同時表示您不編碼和解碼LatitudeLongitude

+0

非常感謝你,它非常簡單。 –

2

你已經證明不編碼LatitudeLongitude代碼...