返回应用程序时,NSCoding数据未保存?

时间:2014-12-29 17:54:12

标签: ios objective-c nsuserdefaults nscoding

我不明白为什么previousArrays会返回(null),我想保存一个包含bezier路径及其颜色的类。

代码:(在调用touchesEnded之后,会创建一个路径并将其保存在内存中。当我使用initWithCoder返回应用时,previousArrays(null) ):

-(id)initWithCoder:(NSCoder *)aDecoder{
    if ( !(self=[super initWithCoder:aDecoder])) return nil;

    CGContextRef context = UIGraphicsGetCurrentContext();
    NSArray *previousArrays = [SaveData loadDict];
    NSLog(@"previousArrays : %@", previousArrays);//***HERE*** : return (null)

    for ( id object in previousArrays){
    //for ( NSDictionary*dict in previousArrays){
        NSLog(@"obj %@", [[object class] description]);

//...

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithCGPath:myPath];//nscoding compliant
    DataForPath *firstPath = [[DataForPath alloc] init];
    firstPath.path = bezierPath;
    firstPath.colorInArray = @(currentColor);
    NSLog(@"touchEnded, firstPath : %@", firstPath);
    NSDictionary *dict = @{@"firstPath":firstPath};

    [SaveData saveDict:dict];
}     


@implementation SaveData

static NSString* kMyData = @"data1";

+ (void) saveDict:(NSDictionary*) dict{
    NSLog(@"saving data...");

    //retrieve previous data
    NSData *previousData = [[NSUserDefaults standardUserDefaults] objectForKey:kMyData];
    NSMutableArray *previousArray = [[NSKeyedUnarchiver unarchiveObjectWithData:previousData] mutableCopy];
    [previousArray addObject:dict];

    //add new data, and save
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:previousArray];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:kMyData];

    //NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
}

+(NSArray*) loadDict {
     NSLog(@"loading data...");
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kMyData];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return array;
}


#import <Foundation/Foundation.h>
@import UIKit;

@interface DataForPath : NSObject <NSCoding>

@property (nonatomic, strong) UIBezierPath* path;
@property (nonatomic, strong) NSNumber *colorInArray;

@end


@implementation DataForPath

- (id)initWithCoder:(NSCoder *)decoder{
    if ( !(self = [super init]) ) return nil;
    self.path = [decoder decodeObjectForKey:@"path"];
    self.colorInArray = [decoder decodeObjectForKey:@"colorInArray"];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder{
    [encoder encodeObject:self.path forKey:@"path"];
    [encoder encodeObject:self.colorInArray forKey:@"colorInArray"];
}

另外,有没有办法查看NSUserDefault中的内容,例如&#34; prettyjson&#34;格式? -dictionaryRepresentation会返回一些类似<124d1f>

的数字

3 个答案:

答案 0 :(得分:0)

您是否检查了saveDict = previousArray的{​​{1}}方法?因为在您保存某些内容之前,您在userDefaults中没有任何内容

答案 1 :(得分:0)

我必须使用它:if ( previousArray == nil ) ...

NSMutableArray *previousArray = [[NSKeyedUnarchiver unarchiveObjectWithData:previousData] mutableCopy];
if ( previousArray == nil ) previousArray = [[NSMutableArray alloc] init];

答案 2 :(得分:-1)

信息不足。

如果对象符合NSCoding协议,则可以将数据转换为数据或从数据转换。您必须为encodeWithCoder和initWithCoder方法编写代码,以保存所有对象的属性。

如果要取消归档使用encodeWithCoder保存的对象,并且其中一个属性未加载,那么您的encodeWithCoder方法很可能存在问题。

发布您要编码的类的标头,所有encodeWithCoder和initWithCoder方法,以及有关您正在编码的对象属性的信息。

同时发布将您的对象转换为数据并保存的代码,并提供有关何时/如何保存的说明。