Oolite
Loading...
Searching...
No Matches
NSUserDefaults+Override.m
Go to the documentation of this file.
1/*
2
3NSUserDefaults+Override.m
4
5Oolite
6Copyright (C) 2004-2025 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
26#import <Foundation/NSData.h>
27#import <Foundation/NSFileManager.h>
28#import <Foundation/NSPropertyList.h>
29
30#ifdef __clang__
31#pragma clang diagnostic push
32#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
33#endif
34@implementation NSUserDefaults (Override)
35
36- (BOOL)writeDictionary:(NSDictionary*)dict
37 toFile:(NSString*)file
38{
39 if ([file length] == 0) {
40 OOLog(@"NSUserDefaultsOverride", @"%@", @"Defaults database filename is empty when writing");
41 } else if (nil == dict) {
42 NSFileManager* mgr = [NSFileManager defaultManager];
43
44 return [mgr removeFileAtPath:file handler:nil];
45 } else {
46 NSData* data;
47 NSString* err;
48
49 err = nil;
50 data = [NSPropertyListSerialization dataFromPropertyList:dict
51 format:NSPropertyListOpenStepFormat
52 errorDescription:&err];
53 if (data == nil) {
54 OOLog(@"NSUserDefaultsOverride", @"Failed to serialize defaults database for writing: %@", err);
55 } else if ([data writeToFile:file atomically:YES] == NO) {
56 OOLog(@"NSUserDefaultsOverride", @"Failed to write defaults database to file: %@", file);
57 } else {
58 return YES;
59 }
60 }
61
62 return NO;
63}
64
65@end
66#ifdef __clang_
67#pragma clang diagnostic pop
68#endif
#define OOLog(class, format,...)
Definition OOLogging.h:84
return nil