質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2414閲覧

MultipeerConnectivity UIAlertController エラー

daigakuse-

総合スコア67

Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/12/09 06:08

プログラミング初心者です。
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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2016/12/09 06:17

UIAlertControllerを表示しなければ落ちないのでしょうか?
daigakuse-

2016/12/09 06:37

すいません、アラートをコメントにしても落ちたので少し原因を考えてみたいと思います。
guest

回答1

0

ベストアンサー

swift

1var enemy_cpu:[UInt8] = [UInt8](repeating:0,count:data.count/MemoryLayout<Int>.size) 2data.copyBytes(to: &enemy_cpu,count: data.count)

これ、Data → [UInt8]に変換しているのだと思うのですが、MemoryLayout<UInt8>ではないでしょうか?
今のままだとArrayのサイズが小さくなってしまうので、次の行で中身をコピーするときにメモリを破壊してしまうような気がします。

//64bit環境で実行 print(MemoryLayout<Int>.size) //=> 8 print(MemoryLayout<UInt8>.size) //=> 1

また、Data → [UInt8]は次のように簡単に書けます。(Swift3から?)

swift

1var enemy_cpu = [UInt8](data)

投稿2016/12/09 06:39

編集2016/12/09 06:43
fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

daigakuse-

2016/12/09 12:47

うまく動作しました!!!ですが、[MCBrowserViewController] MCBrowserViewController shouldn't be receiving this callback こちらのエラーは消えません。。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問