How to append string in local resource txt file for iOS sdk

后端 未结 3 1410
温柔的废话
温柔的废话 2020-12-14 22:48

I have been trying to append strings to a local resource file but I am having trouble finding a solution. I am trying to create a log file for all the function call in my ap

3条回答
  •  盖世英雄少女心
    2020-12-14 23:40

    - (void)log:(NSString *)message {
        NSMutableString *string = [[NSMutableString alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/log.txt", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]]];
        if (!string) string = [[NSMutableString alloc] init];
        [string appendFormat:@"%@\r\n", message];
        [string writeToFile:[NSString stringWithFormat:@"%@/log.txt", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    

提交回复
热议问题