Domanda

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.

È stato utile?

Soluzione

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];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top