在我的应用程序中,每次用户创建新组时,都会创建以下代码(JSON):
{"group a":"0864797073"}
...然后代码保存在目录中名为 groups.txt 的文件中
...但是当用户创建另一个组时,我希望以这种方式修改文件:
{"group a":"0864797073"}
{"group b":"0864797073"}
...进而...
{"group a":"0864797073"}
{"group b":"0864797073"}
{"group c":"0864797073"}
...所以添加一个新行,而不是替换文件。
我怎样才能做到这一点??
在此先感谢您的帮助。
编辑2:
NSString *destDir = [NSString stringWithFormat:@"/Apps/Quick Homework & business/%@/", namegroup.text];
NSString *filename5 = namegroup.text;
NSString *filename6 = @"group";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *groupPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", filename6]];
NSMutableArray *titles = [NSMutableArray array];
[titles addObject:filename5];
NSMutableArray *keys = [NSMutableArray array];
[keys addObject:filename6];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:titles forKeys:keys];
NSString *jsonString = [dict JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding: NSUTF8StringEncoding];
NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:groupPath];
[myHandle seekToEndOfFile];
[myHandle writeData:jsonData];
[myHandle closeFile];
[jsonData writeToFile: groupPath atomically: YES];
[[self restClient] uploadFile:filename6 toPath:destDir
withParentRev:nil fromPath:groupPath];