Bonjour à toutes et tous .
Je ne parviens pas a enregistrer des données (NSMutableArray ) dans mes préférences et sur le bureau .
Merci d'avance .
Je ne parviens pas a enregistrer des données (NSMutableArray ) dans mes préférences et sur le bureau .
Bloc de code:
#import "Controller.h"
@implementation Controller
- (void)awakeFromNib
{
prefs = [[NSUserDefaults standardUserDefaults] retain];
recordsFile = [NSString stringWithString:@"~/Library/Preferences/Les Editions de la Crempse.plist"];
recordsFile = [[recordsFile stringByExpandingTildeInPath] retain];
if ( [prefs arrayForKey:@"Addresses"] != nil ) {
records = [[NSMutableArray alloc] initWithArray:[prefs arrayForKey:@"Addresses"]];
} else {
records = [[NSMutableArray alloc] initWithContentsOfFile:recordsFile];
if ( nil == records ) {
records = [[NSMutableArray alloc] init];
}
}
}
- (void)saveData
{
[records writeToFile:recordsFile atomically:YES];
[prefs setObject:records forKey:@"Addresses"];
[prefs synchronize];
}
- (void)dealloc
{
[self saveData];
[recordsFile release];
[records release];
[prefs release];
recordsFile = nil;
records = nil;
prefs = nil;
[super dealloc];
}
- (NSDictionary *)createRecord
{
NSMutableDictionary *record = [[NSMutableDictionary alloc] init];
[record setObject:[nomField stringValue] forKey:@"Nom"];
[record setObject:[prenomField stringValue] forKey:@"Prenom"];
[record setObject:[emailField stringValue] forKey:@"Email"];
[record setObject:[nudetelField stringValue] forKey:@"N° de Tel"];
[record setObject:[adresseField stringValue] forKey:@"Adresse"];
[record setObject:[cpostalField stringValue] forKey:@"Code Postal"];
[record setObject:[villeField stringValue] forKey:@"Ville"];
[nomField setStringValue:@""];
[prenomField setStringValue:@""];
[emailField setStringValue:@""];
[nudetelField setStringValue:@""];
[adresseField setStringValue:@""];
[cpostalField setStringValue:@""];
[villeField setStringValue:@""];
[record autorelease];
return record;
}
- (IBAction)addRecord:(id)sender
{
[records addObject:[self createRecord]];
[tableView reloadData];
[self saveData];
}
- (IBAction)deleteRecord:(id)sender
{
int status;
NSEnumerator *enumerator;
NSNumber *index;
NSMutableArray *tempArray;
id tempObject;
if ( [tableView numberOfSelectedRows] == 0 )
return;
NSBeep();
status = NSRunAlertPanel(@"Attention!", @"Etes-vous sure de vouloir effacer cette entrée ?", @"OK", @"Cancel", nil);
if ( status == NSAlertDefaultReturn ) {
enumerator = [tableView selectedRowEnumerator];
tempArray = [NSMutableArray array];
while ( (index = [enumerator nextObject]) ) {
tempObject = [records objectAtIndex:[index intValue]];
[tempArray addObject:tempObject];
}
[records removeObjectsInArray:tempArray];
[tableView reloadData];
[self saveData];
}
}
- (IBAction)insertRecord:(id)sender
{
int index = [tableView selectedRow];
[records insertObject:[self createRecord] atIndex:index];
[tableView reloadData];
[self saveData];
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [records count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
id theRecord, theValue;
theRecord = [records objectAtIndex:rowIndex];
theValue = [theRecord objectForKey:[aTableColumn identifier]];
return theValue;
}
- (void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
id theRecord;
theRecord = [records objectAtIndex:rowIndex];
[theRecord setObject:anObject forKey:[aTableColumn identifier]];
[self saveData];
}
@end
Merci d'avance .