0

我的视图中有 3 个标签,这就是我获取传感器数据并将其写入标签中的方式:

func myDeviceMotion(){
        print("Start DeviceMotion")
        motion.deviceMotionUpdateInterval  = 0.5
        motion.startDeviceMotionUpdates(to: OperationQueue.current!) {
            (data, error) in
            print(data as Any)
            if let trueData =  data {

                self.view.reloadInputViews()
                self.xDevi!.text = "x (pitch): \(trueData.attitude.pitch)"
                self.yDevi!.text = "y (roll): \(trueData.attitude.roll)"
                self.zDevi!.text = "z (yaw): \(trueData.attitude.yaw)"
            }
        }
        return
    }

现在我想将该日期保存在 csv 文件中。我怎样才能做到这一点?

我已经尝试过这个答案中提出的解决方案:如何记录传感器数据并导出到 CSV?,但它对我不起作用。

4

1 回答 1

0
// Open a new file for writing using FileManager (may have to remove an old one)
let fh: FileHandle = ... // open the file

// Header first (optional)
var s = "Pitch,Roll,Yaw\n" // note no spaces, comma is the operator
fh.writeData(s.data(using: .utf))

// then for each line of dat
s = String(format: "%f,%f,%f\n", trueData.attitude.pitch, trueData.attitude.roll, trueData.attitude.yaw)
fh.writeData(s.data(using: .utf))

// when done, close the file using fh.closefile()
于 2020-02-21T13:35:14.427 回答