2014-12-29 24 views
0

我不明白爲什麼previousArrays返回(null),我想保存一個包含bezier路徑和它的顏色的類。返回到應用程序時未保存NSCoding數據?

代碼:(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中的內容,例如「prettyjson」格式? -dictionaryRepresentation返回一些數字像<124d1f>

+0

貴'DataForPath'類實現'NSCoding'? – Chris

+0

@Chris嗨克里斯,是的,代碼如下,當您向下滾動時,DataForPath:NSObject Paul

+0

您還應該符合['NSSecureCoding'協議](https://developer.apple.com/library/ios/文檔/基礎/參考/ NSSecureCoding_Protocol_Ref/index.html)在iOS 6和更高版本中可用。 – jww

回答

0

我不得不用這樣的:if (previousArray == nil) ...

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

您應該接受自己的回答,以便將其置於回答列表的頂部。請參閱Meta上的[接受答案如何工作?](http://meta.stackexchange.com/q/5234/173448)。 – jww

0

你有沒有檢查saveDict方法previousArray == nil?,因爲你保存的東西之前,你有沒有在userDefaults

+0

對舊評論感到抱歉,它的工作原理,我忘了在「saveData」中說「如果previousArray是零,分配init一個新的」。我正在向一個空對象添加一個對象。棘手。感謝你的信息。 – Paul

-1

沒有足夠的信息。

如果對象符合NSCoding協議,則可以將數據轉換爲數據或從數據轉換而來。您必須爲保存所有對象屬性的encodeWithCoder和initWithCoder方法編寫代碼。

如果你正在取消一個你用encodeWithCoder保存的對象並且它的一個屬性沒有被加載,那麼你的encodeWithCoder方法很可能存在問題。

發佈您要嘗試編碼的類的頭文件,所有encodeWithCoder和initWithCoder方法以及有關您正在編碼的對象屬性的信息。

還發布代碼,將您的對象轉換爲數據並保存它,並提供關於何時/如何保存的描述。

+0

對舊評論感到遺憾,它的工作原理,我忘了在「saveData」中說「如果previousArray是零,分配init一個新的」。我正在向一個空對象添加一個對象。棘手。感謝你的信息。 – Paul

+0

@保羅,我很高興你解決了你的問題。你可能應該回答自己的問題並接受它,以便人們知道它已經解決了,並且可以幫助其他讀者。 –

+0

*「如果對象符合NSCoding協議,可以將數據轉換爲數據或從數據轉換出來......」* - 他還應該符合['NSSecureCoding'協議](https://developer.apple.com/library/ios /documentation/Foundation/Reference/NSSecureCoding_Protocol_Ref/index.html)適用於iOS 6及以上版本。 – jww

相關問題