1

EDIT: Ok i decided to save the array in the userDefaults... should be easy, right ? Save:

NSUserDefaults *userDefs = [NSUserDefaults standardUserDefaults];
    [userDefs setObject:videoArray forKey:@"dataArray"];
    [userDefs synchronize];

Load:

 NSUserDefaults *userDefs = [NSUserDefaults standardUserDefaults];
    videoArray = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"dataArray"];

    [tableview reloadData];
    NSLog(@"%@",videoArray);

Class of the objects which are in the array:

@interface DEVideoModel : NSObject

@property (copy) NSString *name;
@property (copy) NSImage *thumbnail;
@property (copy) NSDictionary *qualities;
@property (readwrite) float videoSize;
@property (readwrite) float progress;
@property (copy) NSString *filePath;
@property (copy) NSDate *datum;


@end

@synthesize name,filePath,videoSize,qualities,thumbnail,datum,progress;
-(id)init {
    self = [super init];
    if(self) {
        qualities = [[NSDictionary alloc]init];
        thumbnail = [[NSImage alloc]init];
    }
    return self;
}


@end

And my videoArray is (null) when i load it ?! I don't get it. videoArray is a NSMutableArray not NSArray by the way.

dehlen
  • 7,325
  • 4
  • 43
  • 71
  • is encodeWithcoder method getting called? – Anoop Vaidya Mar 24 '13 at 16:26
  • ah i see, encodeWithCoder is not getting called. Did i miss something ? In my .h i implemented – dehlen Mar 24 '13 at 16:30
  • Side note: Did you create the `NSApplicationSupportDirectory`? This directory does not exist by default in the app sandbox. Also, do not use string formats to create paths. Use `NSString *path = [appSupportDir stringByAppendingPathComponent:@"DEConvert.dat"];` – rmaddy Mar 24 '13 at 16:32
  • hm ok, i will change the way i get the path but the file is saved in this directory. (At the moment my app is not sandboxed) – dehlen Mar 24 '13 at 16:34
  • @DavidEhlen Sorry, the comment about the directory not existing is true for iOS, not OSX. Please ignore that part. – rmaddy Mar 24 '13 at 16:35
  • Got it ! Have a look at this topic: http://stackoverflow.com/questions/537044/storing-custom-objects-in-an-nsmutablearray-in-nsuserdefaults – dehlen Mar 24 '13 at 17:29
  • You can only store property lists in user defaults. A DEVideoModel is not a property list; therefore, an array of them is not a property list, either. Hence Dipen Panchasara's suggestion of archiving and unarchiving: the data that archiving produces *is* a property list, so an array of those is also a property list. The part he left out is that you need to make DEVideoModel conform to NSCoding. – Peter Hosey Mar 24 '13 at 20:48

1 Answers1

2

IN your code you are writting NSData to NSCoder, so you need to read NSData then convert it to Array.

NSURL *appSupportDir = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
NSString *path = [NSString stringWithFormat:@"%@/DEConvert.dat",[appSupportDir path]];
NSLog(@"%@",appSupportDir);
NSData *data = [NSData dataWithContentsOfFile:path];
NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];

to store object in NSUserDefault

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"your key"];

Unarchiving is just as easy:

NSData *NewData = [[NSUserDefaults standardUserDefaults] objectForKey:@"your key"];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:NewData];
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • I get this output: *** -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL – dehlen Mar 24 '13 at 16:51
  • i have edited answer, i forgot to add filename in path which we are reading. please try now, sorry for inconvenience. best of luck – Dipen Panchasara Mar 24 '13 at 16:54
  • Yeah i saw that and corrected it myself. Now the output is just fine (no more error) but the saving is definetly false. I had a look at the saved .dat File: `// !!! BINARY PROPERTY LIST WARNING !!! // // The pretty-printed property list below has been created // from a binary version on disk and should not be saved as // the ASCII format is a subset of the binary representation! // { "$archiver" = "NSKeyedArchiver"; "$objects" = ( "$null" ); "$top" = { root = :false; }; "$version" = 100000; }` – dehlen Mar 24 '13 at 16:56
  • dear friend why are you saving it in file. save it in NSUserDefault. let me give you a sample code for it. – Dipen Panchasara Mar 24 '13 at 17:01
  • i thought that would be the "right clean" way. If its acceptable to save it in NSUserDefaults i think i will get that working on my own – dehlen Mar 24 '13 at 17:04
  • thats great. good luck. if amount of data is less then you should go for it. it was nice conversation with you. feel free to ask anything. – Dipen Panchasara Mar 24 '13 at 17:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26820/discussion-between-dipen-panchasara-and-david-ehlen) – Dipen Panchasara Mar 24 '13 at 17:16