在Objective C中,我有一大堆代码以我使用ofstream指定的格式写入CSV文件:
std::ofstream ofs;
const char header[] = "Time Stamp(YYYY-MM-DD HH:MM:SS),Engagement, Theta AF3, Theta AF4, Theta T7, Theta T8, Theta Pz, Alpha AF3, Alpha AF4, Alpha T7, Alpha T8, Alpha Pz, Beta AF3, Beta AF4, Beta T7, Beta T8, Beta Pz, Gamma AF3, Gamma AF4, Gamma T7, Gamma T8, Gamma Pz";
-(void)getResults{
for(int i = 0; i<5; i++){
if(i!=3){
ofs<<AF3[i]<<",";
ofs<<AF4[i]<<",";
ofs<<T7[i]<<",";
ofs<<T8[i]<<",";
ofs<<Pz[i]<<",";
}
}
}
-(void)startProgram:(NSString *)firstName next:(NSString *)lastName{
// thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(getInsightResult) object:nil];
//thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(updateLabel) object:nil];
//thread1.start;
/*************File .csv stored in folder Document of Application********************/
NSString *path_file_csv = [NSString stringWithFormat:@"%@/AlphaTest_%@_%@.csv",documentDirectory,lastName,firstName];
NSLog(@"Path to file .csv %@",path_file_csv);
ofs.open([path_file_csv cStringUsingEncoding:NSUTF8StringEncoding]);
ofs << header << std::endl;
[self performSelectorInBackground:@selector(getInsightResult) withObject:nil];
NSTimer *oneSecTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}
因此,我每次用ofs写一个单元格。这允许我逐个创建标题和输入值到单元格。我如何仅使用swift代码快速执行此操作?
答案 0 :(得分:1)
看起来您使用的是Objective-C ++,您仍然可以使用Objective-C ++并从Swift访问它。或者对于纯Swift解决方案,您可以使用OutputStream的init(toFileAtPath:append :)。
有关详细信息,请参阅Apple的文档:https://developer.apple.com/reference/foundation/outputstream