Objective c 如何获取要写入文本文件的数据?
下面是我用于控制台输出的方法。如何输出到文本文件Objective c 如何获取要写入文本文件的数据?,objective-c,append,Objective C,Append,下面是我用于控制台输出的方法。如何输出到文本文件 @implementation GetMoves - (id)initWithDevice:(Device *)device{ if(self = [super init]){ _device = device; } return self; } - (void)Render{ [super ClearConsole]; printf("MOVES INFO\n");
@implementation GetMoves
- (id)initWithDevice:(Device *)device{
if(self = [super init]){
_device = device;
}
return self;
}
- (void)Render{
[super ClearConsole];
printf("MOVES INFO\n");
printf("==================================\n");
NSArray* data = [_device GetData:NULL];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
if(data != nil){
for(int i = 0; i < data.count; i++){
DataPoint* current = [data objectAtIndex:i];
NSMutableString* output = [[NSMutableString alloc] init];
[output appendFormat:@"%d",(i+1)];
[output appendString:@" - "];
[output appendString:[dateFormatter stringFromDate:current.RecordedAt]];
[output appendString:@" - "];
[output appendFormat:@"%d",current.StepCount];
printf("%s\n", [output UTF8String]);
}
}
else
printf("No new data found on device.\n");
printf("\n");
printf("==================================\n");
printf("\n");
printf("Press ENTER to continue.");
fgetc(stdin);
}
@end
@GetMoves实现
-(id)initWithDevice:(设备*)设备{
if(self=[super init]){
_装置=装置;
}
回归自我;
}
-(无效)渲染{
[超级ClearConsole];
printf(“移动信息”);
printf(“==========================================================\n”);
NSArray*data=[\u设备GetData:NULL];
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[日期格式化程序setDateFormat:@“yyyy-MM-dd HH:MM:ss”];
如果(数据!=nil){
对于(int i=0;i
您可以简单地编写为:
NSString *path = [@"~" stringByExpandingTildeInPath] ;
//change your path here
path=[path stringByAppendingString:@"/Documents/*REPORTS/"];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/TWCodeFile.txt",path];
//create content - four lines of text
NSString *content = @"The content of file, can be more by appending string.";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
为什么在同一个代码中使用
appendFormat
和printf()
?检查我的编辑,有两个错误是由于某个对象造成的,而不是在这堆代码中,你应该猜到并理解了。我是这样写的吗?[fileManager createDirectoryAtPath:WithMediateDirectory:属性:错误:createFileAtPath:内容:属性:nil]@TWcode:检查此代码,这将在/Documents/*REPORTS中创建一个名为TWCodeFile.txt的文件/