2017-02-18 27 views
-3

在目標CI有這個代碼塊是寫在格式CSV文件,我還指定使用的ofstream:如何在swift中寫入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代碼快速執行此操作?

回答

1

看起來你使用的是Objective-C++,你仍然可以使用Objective-C++並從Swift中訪問它。或者對於純Swift解決方案,您可以使用OutputStream的init(toFileAtPath:append :)。

有關更多詳細信息,請參閱Apple文檔:https://developer.apple.com/reference/foundation/outputstream