0

I'm trying to rewrite a file from which I get data I operate with. But I get an error which says that I have no permission to alter it or delete.

Here I'm getting data:

- (id) init {
if (self = [super init]) {
    NSError *error = nil;
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"slot_reader_source" ofType:@"txt"];
    NSURL *url = [NSURL fileURLWithPath:filePath];

    data = [NSMutableData dataWithContentsOfFile:filePath];
    allData = [[NSMutableDictionary alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData:data
                                                                                              options:kNilOptions
                                                                                                error:&error]];
}
return self;
}

Here I'm trying to change that file

- (void) addNewWord: (NSString *) word {
NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSArray *alphabet = [[allData objectForKey:@"words"] objectForKey:[NSString stringWithFormat:@"%@%d", [[NSUserDefaults standardUserDefaults] objectForKey:@"language"], 1]];
NSMutableString *aphabetString = [[NSMutableString alloc] initWithCapacity:1];
for (NSString *letter in alphabet) {
    [aphabetString appendString:letter];
}
NSCharacterSet *allowedSymbols = [NSCharacterSet characterSetWithCharactersInString:aphabetString];
word = [[[word componentsSeparatedByCharactersInSet:allowedSymbols] componentsJoinedByString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];

NSInteger size = [word length];
NSMutableArray *thisSizeLangWordsBuffer = [[NSMutableArray alloc] initWithArray:[[allData objectForKey:@"words"] objectForKey:[NSString stringWithFormat:@"%@%d", language, size]]];
[thisSizeLangWordsBuffer addObject:word];
NSMutableDictionary *newData = [[NSMutableDictionary alloc] initWithDictionary:[allData objectForKey:@"words"]];

[newData setObject:thisSizeLangWordsBuffer forKey:[NSString stringWithFormat:@"%@%d", [[NSUserDefaults standardUserDefaults] objectForKey:@"language"], size]];
NSMutableDictionary *newAllData = [[NSMutableDictionary alloc] initWithDictionary:allData];
[newAllData setObject:newData forKey:@"words"];
//allData = nil;
[allData setDictionary:newAllData];

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"slot_reader_source" ofType:@"txt"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSError* error = nil;
data = [NSMutableData dataWithData:[NSJSONSerialization dataWithJSONObject:newAllData options:NSJSONWritingPrettyPrinted error:nil]];
[[NSFileManager defaultManager] removeItemAtURL:url error:&error];
BOOL res = [data writeToFile:filePath options:NSDataWritingAtomic error: &error];
NSString *newText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

}
AOY
  • 355
  • 3
  • 22
  • 3
    Files in the bundle are read only. Copy the file from the bundle to a directory if you need to edit it. – Gruntcakes May 03 '16 at 14:26
  • Thank you, can you add that as an answer so that I can select it as a right answer – AOY May 04 '16 at 08:24
  • 1
    @AnastasiaY: No it's better that the question get flagged as duplicate than post a answer which is the same as another one. (Gather all answer in the same spot, with potentially more details). – Larme May 04 '16 at 09:33

0 Answers0