我有一个NSMutableArray里充满了我要救我的电影类至极的对象,但它不工作,我想不通为什么工作...不NSUserDefaults的使用的NSKeyedArchiver
Movie.h:
@interface Movie : NSObject <NSCoding>{
NSString *name;
int year;
int length;
NSString *file_size;
int rating;
NSArray *genre;
NSString *plot;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) int year;
@property (nonatomic, assign) int length;
@property (nonatomic, retain, retain) NSString *file_size;
@property (nonatomic, assign) int rating;
@property (nonatomic, retain) NSArray *genre;
@property (nonatomic, retain) NSString *plot;
-(id) initWithName:(NSString*)newName year:(int)newYear length:(int)newLength filesize:(NSString*)newFileSize rating:(int)newRating genre:(NSArray*)newGenre plot:(NSString*)newPlot;
- (void) encodeWithCoder : (NSCoder *)encode ;
- (id) initWithCoder : (NSCoder *)decode;
@end
Movie.m:
@implementation Movie
@synthesize name;
@synthesize year;
@synthesize length;
@synthesize file_size;
@synthesize rating;
@synthesize genre;
@synthesize plot;
-(id)initWithName:(NSString *)newName year:(int)newYear length:(int)newLength filesize:(NSString *)newFileSize rating:(int)newRating genre:(NSArray *)newGenre plot:(NSString *)newPlot{
self.name = newName;
self.year = newYear;
self.length = newLength;
self.file_size = newFileSize;
self.rating = newRating;
self.genre = newGenre;
self.plot = newPlot;
return self;
}
- (void)encodeWithCoder:(NSCoder *)encode;
{
[encode encodeObject:name forKey:@"name"];
[encode encodeInt32:year forKey:@"year"];
[encode encodeInt32:length forKey:@"length"];
[encode encodeObject:file_size forKey:@"file_size"];
[encode encodeInt32:rating forKey:@"rating"];
[encode encodeObject:genre forKey:@"genre"];
[encode encodeObject:plot forKey:@"plot"];
}
- (id)initWithCoder:(NSCoder *)decode;
{
NSString *name_decode = [decode decodeObjectForKey:@"name"];
int year_decode = [decode decodeInt32ForKey:@"year"];
int length_decode = [decode decodeInt32ForKey:@"length"];
NSString *file_size_decode = [decode decodeObjectForKey:@"file_size"];
int rating_decode = [decode decodeInt32ForKey:@"rating"];
NSArray *genre_decode = [decode decodeObjectForKey:@"genre"];
NSString *plot_decode =[decode decodeObjectForKey:@"plot"];
return [self initWithName:name_decode year:year_decode length:length_decode filesize:file_size_decode rating:rating_decode genre:genre_decode plot:plot_decode];
}
@end
保存操作(电影是包含我的对象的NSMutableArray的):
NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:Movies];
[userDefault setObject:encodedData forKey:[NSString stringWithFormat:@"MOVIES"]];
荷载作用:
NSData *decodedData = [userDefault objectForKey: [NSString stringWithFormat:@"MOVIES"]];
NSArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: decodedData];
返回的数组始终是(空)......我不知道 我尝试了几种不同类型的代码片段,我发现在互联网上和/或计算器
谢谢@rmaddy!那是为我做的! :)) BTW,电影不是问题,它一定是解码/编码选择器 – MothaFvckaJones