0

カスタム クラスからの保存に問題があります。コードのどこかからエラーを NSUserDefaults に保存しようとしましたが、うまくいきません。

H ファイル:

#import <Foundation/Foundation.h>
@interface Errors : NSObject<NSCoding>
@property (nonatomic,strong) NSString*  type;
@property (nonatomic,strong) NSString*  verNum;
@property (nonatomic,strong) NSString*  title;
@property (nonatomic,strong) NSString*  content;
@property (nonatomic,strong) NSString*  source;
@property (nonatomic,strong) NSString*  userId;
@property (nonatomic,strong) NSNumber*  videoId;

@end

M ファイル:

#import "Errors.h"
@implementation Errors
@synthesize verNum;
@synthesize type;
@synthesize title;
@synthesize content;
@synthesize source;
@synthesize userId;
@synthesize videoId;

- (void)encodeWithCoder:(NSCoder *)encoder{
    [encoder encodeObject:self.verNum forKey:@"verNum"];
    [encoder encodeObject:self.type forKey:@"type"];
    [encoder encodeObject:self.title forKey:@"title"];
    [encoder encodeObject:self.content forKey:@"content"];
    [encoder encodeObject:self.source forKey:@"source"];
    [encoder encodeObject:self.userId forKey:@"userId"];
    [encoder encodeObject:self.videoId forKey:@"videoId"];
}

- (id)initWithCoder:(NSCoder *)decoder{
    self = [super init];
    if( self != nil ) {
        self.verNum = [decoder decodeObjectForKey:@"verNum"];
        self.type = [decoder decodeObjectForKey:@"type"];
        self.title = [decoder decodeObjectForKey:@"title"];
        self.content = [decoder decodeObjectForKey:@"content"];
        self.source = [decoder decodeObjectForKey:@"source"];
        self.userId = [decoder decodeObjectForKey:@"userId"];
        self.videoId = [decoder decodeObjectForKey:@"videoId"];
    }
    return self;
}
@end

保存:

errorsDitalis = [[Errors alloc]init];
errorsDitalis.verNum = (NSString *) [[request userInfo] objectForKey:@"verNum"];
errorsDitalis.type =(NSString *)  [[request userInfo] objectForKey:@"type"];
errorsDitalis.title =(NSString *)  [[request userInfo] objectForKey:@"title"];
errorsDitalis.content =(NSString *)  [[request userInfo] objectForKey:@"content"];
errorsDitalis.source =(NSString *)  [[request userInfo] objectForKey:@"source"];
errorsDitalis.userId =(NSString *)  [[request userInfo] objectForKey:@"userId"];
errorsDitalis.videoId =(NSNumber *)  [[request userInfo] objectForKey:@"videoId"];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:errorsDitalis forKey:@"ErrorList"];
[userDefaults synchronize];

読み込み中:

Errors * newError = [[Errors alloc]init];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
newError = [userDefaults objectForKey:@"ErrorList"];

newErrors に対して nil を返すだけです。このヘルプを使用しようとしましたが、うまくいきませんでした。誰かがアイデアを持っていますか?

4

2 に答える 2

0

試す

[self setVerNum:[decoder decodeObjectForKey:@"verNum"]];

それ以外の

self.verNum = [decoder decodeObjectForKey:@"verNum"];

電話するときはセッターを使うべきだと思いますinitWithCoder

于 2012-11-05T08:58:46.577 に答える
0

私はすべきだと思います:

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self.errorsDitalis] forKey:@"ErrorList"];
    [[NSUserDefaults standardUserDefaults] synchronize];

ではない:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:errorsDitalis forKey:@"ErrorList"];
[userDefaults synchronize];

そしてロード:

newError = (Errors *)[NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"ErrorList"]];
于 2012-11-05T09:12:37.780 に答える