swiftでiOSアプリを作成しています。
取得したセンサのデータをPCに送信し、解析結果をPCに送信しています。
ですが、このようなものが出てきます。
なにか改善点があればご教授ください。
出たエラー UILabel.text must be used from main thread only viewContoroller.swiftの部分に出ます。 if self.kantou=="1"{ self.resultLabel.text="少しあります。" }else if self.kantou=="2"{ self.resultLabel.text="大丈夫です。" }else if self.kantou=="3"{ self.resultLabel.text="なにも異常ないです。" } }
ViewContoroller.swift
1// 2// ViewController.swift 3 4 5 6import UIKit 7import CoreMotion 8import simd 9 10class ViewController: UIViewController { 11 12 13 14 @IBOutlet weak var label: UILabel! 15 // ボタンの定義 16 var acc_x:String="" 17 var acc_y:String="" 18 var acc_z:String="" 19 20 var gra_x:String="" 21 var gra_y:String="" 22 var gra_z:String="" 23 24 var gyro_x:String="" 25 var gyro_y:String="" 26 var gyro_z:String="" 27 28 var atti_roll:String="" 29 var atti_pitch:String="" 30 var atti_yaw:String="" 31 32 var quat_x:String="" 33 var quat_y:String="" 34 var quat_z:String="" 35 var quat_w:String="" 36 37 var kantou:String="" 38 @IBOutlet weak var resultLabel: UILabel! 39 40 //サンプリングレート 41 var deviceMotionUpdateInterval = 0 42 43 44 //答えのテキスト 45// var resultText:String = "" 46 47 //日にち取得 48 let format = DateFormatter() 49 50 private(set) var isRecording = false 51 52 //記録するテキスト 53 private var recordText = "" 54 55 let motionManager: CMMotionManager = CMMotionManager() 56 57 58 override func viewDidLoad() { 59 super.viewDidLoad() 60 _=OperationQueue() 61 } 62 override func didReceiveMemoryWarning() { 63 super.didReceiveMemoryWarning() 64 } 65 66 67 68 69 //スタートボタンで計測中 70 @IBAction func tap_Start(_ sender: Any) { 71 motionManager.deviceMotionUpdateInterval = 0.016666666 // 60Hz サンプリングレート 72 recordText = "" 73 // recordText += headerText + "\n" 74 isRecording = true 75 76 //取得に移動 77 getMotionDate() 78 79 label.text = "計測中" 80 view.backgroundColor = UIColor.red 81 } 82 83 //ストップボタンで計測ストップ 84 @IBAction func tap_Stop(_ sender: Any) { 85 if motionManager.isDeviceMotionAvailable{ 86 motionManager.stopDeviceMotionUpdates() 87 } 88 89 isRecording = false 90 91 format.dateFormat = "yyyy-MMdd-HHmmss" //時間の取得 92 let dateText = format.string(from: Date()) 93 showSaveCsvFileAlert(fileName: dateText) 94 95 label.text = " " 96 view.backgroundColor = UIColor.white 97 } 98 99 //モーションセンサの取得 100 func getMotionDate(){ 101 if motionManager.isDeviceMotionAvailable{ 102 motionManager.startDeviceMotionUpdates(to:OperationQueue.current!, withHandler:{deviceManager, error in 103 let accel: CMAcceleration = deviceManager!.userAcceleration 104 self.acc_x = String(format: "%.2f", accel.x) 105 self.acc_y = String(format: "%.2f", accel.y) 106 self.acc_z = String(format: "%.2f", accel.z) 107 108 let gravity: CMAcceleration = deviceManager!.gravity 109 self.gra_x = String(format: "%.2f", gravity.x) 110 self.gra_y = String(format: "%.2f", gravity.y) 111 self.gra_z = String(format: "%.2f", gravity.z) 112 113 let gyro: CMRotationRate = deviceManager!.rotationRate 114 self.gyro_x = String(format: "%.2f", gyro.x) 115 self.gyro_y = String(format: "%.2f", gyro.y) 116 self.gyro_z = String(format: "%.2f", gyro.z) 117 118 let attitude: CMAttitude = deviceManager!.attitude 119 self.atti_roll = String(format: "%.2f", attitude.roll) 120 self.atti_pitch = String(format: "%.2f", attitude.pitch) 121 self.atti_yaw = String(format: "%.2f", attitude.yaw) 122 123 let quaternion: CMQuaternion = attitude.quaternion 124 self.quat_x = String(format: "%.2f", quaternion.x) 125 self.quat_y = String(format: "%.2f", quaternion.y) 126 self.quat_z = String(format: "%.2f", quaternion.z) 127 self.quat_w = String(format: "%.2f", quaternion.w) 128 129 130 if self.isRecording { 131 self.format.dateFormat = "MMddHHmmssSSS" 132 133 var text = "" 134 135 text += String(accel.x) + "," 136 text += String(accel.y) + "," 137 text += String(accel.z) + "," 138 text += String(gravity.x) + "," 139 text += String(gravity.y) + "," 140 text += String(gravity.z) + "," 141 text += String(gyro.x) + "," 142 text += String(gyro.y) + "," 143 text += String(gyro.z) + "," 144 text += String(quaternion.x) + "," 145 text += String(quaternion.y) + "," 146 text += String(quaternion.z) + "," 147 text += String(quaternion.w) 148 149 self.addRecordText(addText: text) 150 151 } 152 }) 153 } 154 } 155 156 func addRecordText(addText:String) { 157 recordText += addText + "\n" 158 } 159 160 161 func showSaveCsvFileAlert(fileName:String){ 162 163 let alertController = UIAlertController(title: "CSVデータを保存しまし", message: "名前を入力してください", preferredStyle: .alert) //ファイル名の入力するアラートの 164 165 let defaultAction:UIAlertAction = 166 UIAlertAction(title: "はい", 167 style: .default, 168 handler:{ 169 (action:UIAlertAction!) -> Void in 170 171 //テキストフィールドに入力したものを格納 172 let textField = alertController.textFields![0] as UITextField 173 self.saveSensorDataToCsv(fileName: textField.text!) 174 175 176 self.tuusin() 177 }) 178 179 let cancelAction:UIAlertAction = 180 UIAlertAction(title: "いいえ", 181 style: .cancel, 182 handler:{ 183 (action:UIAlertAction!) -> Void in 184 self.showDeleteRecordedDataAlert(fileName: fileName) 185 }) 186 187 alertController.addTextField { (textField:UITextField!) -> Void in 188 alertController.textFields![0].text = fileName 189 } 190 alertController.addAction(defaultAction) 191 alertController.addAction(cancelAction) 192 193 present(alertController, animated: true, completion: nil) 194 } 195 196 197 //取得したデータを削除するorしないのアラートを出力 198 func showDeleteRecordedDataAlert(fileName:String){ 199 let alertController = UIAlertController(title: "記録したデータを削除します", message: "削除してよろしいですか?", preferredStyle: .alert) 200 201 let defaultAction:UIAlertAction = 202 UIAlertAction(title: "はい", 203 style: .default, 204 handler:{ 205 (action:UIAlertAction!) -> Void in 206 // delete recorded data 207 }) 208 let cancelAction:UIAlertAction = 209 UIAlertAction(title: "いいえ", 210 style: .cancel, 211 handler:{ 212 (action:UIAlertAction!) -> Void in 213 self.showSaveCsvFileAlert(fileName: fileName) 214 }) 215 alertController.addAction(defaultAction) 216 alertController.addAction(cancelAction) 217 218 present(alertController, animated: true, completion: nil) 219 } 220 221 222 //csvデータの保存 223 func saveSensorDataToCsv(fileName:String) { 224 let filePath = NSHomeDirectory() + "/Documents/" + fileName + ".csv" 225 226 do{ 227 try recordText.write(toFile: filePath, atomically: false, encoding: String.Encoding.utf8) 228// print("Success to Write CSV") 229// print(filePath) 230 231 }catch let error as NSError{ 232 print("Failure to Write CSV\n(error)") 233 } 234 } 235 236 237 238 func tuusin(){ 239 240 let url = URL(string: "http://○○○○○○○○:8000") 241 var request = URLRequest(url: url!) 242 // POSTを指定 243 request.httpMethod = "POST" 244 // POSTするデータをBodyとして設定 245 246 247 request.httpBody = recordText.data(using: .utf8) 248 let session = URLSession.shared 249 session.dataTask(with: request) { (data, response, error) in 250 if error == nil, let data = data, let response = response as? HTTPURLResponse { 251 // HTTPヘッダの取得 252 print("Content-Type: (response.allHeaderFields["Content-Type"] ?? "")") 253 // HTTPステータスコード 254 print("statusCode: (response.statusCode)") 255 256 self.kantou=String(data: data, encoding: .utf8) ?? "" 257 print(self.kantou) 258 259 if self.kantou=="1"{ 260 self.resultLabel.text="少しあります。" 261 262 }else if self.kantou=="2"{ 263 self.resultLabel.text="大丈夫です。" 264 265 }else if self.kantou=="3"{ 266 self.resultLabel.text="なにも異常ないです。" 267 268 } 269 270 } 271 }.resume() 272 } 273 274}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/19 02:43