プログラミング初心者です。
multipeerconnectivity を使いターン制の通信ゲームを作っています。
データを受信したらUIAlertControllerでアラートを表示したいのですが、エラーが起きて表示されません。検索してもでてこないので困っています。
表示されないアラートは実行時によって違います。
###エラー
2016-12-09 14:37:24.926418 MM[1309:274646] [MCBrowserViewController] MCBrowserViewController shouldn't be receiving this callback. MM(1309,0x1b74e7c40) malloc: *** error for object 0x170044140: Invalid pointer dequeued from free list *** set a breakpoint in malloc_error_break to debug
###該当コード
swift
1 //判定アラート 2 func ME(){ 3 //勝敗判定 4 self.math(forecast:self.forecast_cpu, math:self.mine, &self.cpu_task, &self.cpu_best) 5 6 let alert5: UIAlertController = UIAlertController(title: "O", message:"O", preferredStyle:UIAlertControllerStyle.alert) 7 // ② Actionの設定 8 let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style:.default,handler:{ 9 // ボタンが押された時の処理を書く(クロージャ実装) 10 (action:UIAlertAction!) -> Void in 11 12 let alert6: UIAlertController = UIAlertController(title: "あなたの番", message:おオ", preferredStyle:UIAlertControllerStyle.alert) 13 //Actionの設定 14 let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style:.default,handler:{ 15 // ボタンが押された時の処理を書く(クロージャ実装) 16 (action:UIAlertAction!) -> Void in 17 //TableView更新 18 self.make_cpu_cells() 19 }) 20 //UIAlertControllerにActionを追加 21 alert6.addAction(cancelAction) 22 //Alertを表示 23 24 self.present(alert6, animated: true, completion: nil) 25 26 }) 27 //UIAlertControllerにActionを追加 28 alert5.addAction(cancelAction) 29 // ④ Alertを表示 30 present(alert5, animated: true, completion: nil) 31 32 } 33 34 //後攻のアラート 35 func alertfirst(){ 36 37 //UIAlertControllerクラスのインスタンスを生成 38 let alert: UIAlertController = UIAlertController(title: "O", message: "O", preferredStyle:UIAlertControllerStyle.alert) 39 40 //Actionの設定 41 let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style:.default,handler:{ 42 // ボタンが押された時の処理を書く(クロージャ実装) 43 (action:UIAlertAction!) -> Void in 44 }) 45 46 //UIAlertControllerにActionを追加 47 alert.addAction(cancelAction) 48 49 //Alertを表示 50 present(alert, animated: true, completion: nil) 51 52 } 53 54 //先攻アラート 55 func alertfirst2(){ 56 //UIAlertControllerクラスのインスタンスを生成 57 let alert10: UIAlertController = UIAlertController(title: "スタート!", message: "O。", preferredStyle:UIAlertControllerStyle.alert) 58 59 //Actionの設定 60 let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style:.default,handler:{ 61 // ボタンが押された時の処理を書く(クロージャ実装) 62 (action:UIAlertAction!) -> Void in 63 }) 64 65 //UIAlertControllerにActionを追加 66 alert10.addAction(cancelAction) 67 68 //Alertを表示 69 present(alert10, animated: true, completion: nil) 70 } 71 72 73 func session(_ session: MCSession, didReceive data: Data, 74 fromPeer peerID: MCPeerID) { 75 // Called when a peer sends an NSData to us 76 // This needs to run on the main queue 77 DispatchQueue.main.async { 78 var enemy_cpu:[UInt8] = [UInt8](repeating:0,count:data.count/MemoryLayout<Int>.size) 79 data.copyBytes(to: &enemy_cpu,count: 80 data.count) 81 if(self.t==0 && self.t2 == 0){ 82 //アラート表示 83 self.alertfirst() 84 }else if(self.t==1 && self.t2 != 100){ 85 //アラート表示 86 self.alertfirst2() 87 }else{ 88 //アラート表示 89 self.ME() 90 } 91 } 92 } 93 } 94
###試したこと
main Thread以外でUIviewを操作するといけないとかなんとかあったので、DispatchQueue.main.asyncを追加してみたがうまくいかなかった。
###補足情報(言語/FW/ツール等のバージョンなど)
iOS 10.11
xcode8.1
回答1件
あなたの回答
tips
プレビュー