2017-11-09 9 views
1

私は、objective-cからswiftに挑戦しようとしています。Objective-CからSwift VCに挑戦しようとしています

しかし、私は次のエラー

enter image description here

を受けるの下に、私は、オブジェクトを使用して、これをしようとしたとき、私は私が間違ってやっているかわからない、私は、ストーリーボード上のセットアップセグエをしましたし、割り当てられましたそれは同じIDに、準備関数と実行を作成しました。

- (void)renderer:(id<SCNSceneRenderer>)renderer willRenderScene:(SCNScene *)scene atTime:(NSTimeInterval)time { 
    // Look for trackables, and draw on each found one. 
    size_t trackableCount = trackableIds.size(); 
    for (size_t i = 0; i < trackableCount; i++) { 
//  NSLog(@"this is the variable value: %d", trackableIds[i]); 
     // Find the trackable for the given trackable ID. 
     ARTrackable *trackable = arController->findTrackable(trackableIds[i]); 
//  SCNCamera *camera = self.cameraNode.camera; 
     SCNNode *trackableNode = self.trackableNodes[i]; 
     if (trackable->visible) { 
     if (trackableIds[i] == 0) { 
      NSLog(@"Starbucks"); 
      [self performSegueWithIdentifier:@"brandSegue" sender:self]; 
     } else if (trackableIds[i] == 1) { 
      NSLog(@"Dortios"); 
     } 
     } else { 
     trackableNode.opacity = 0; 
     } 

    } 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"brandSegue"]) { 
    JSONViewController *destViewController = segue.destinationViewController; 
    } 
} 

EDIT - チャットでの議論後、エラーメッセージ

2017-11-09 16:57:05.232992+0000 MyARApp[2573:1099927] *** Assertion failure in -[UIApplication _cachedSystemAnimationFenceCreatingIfNecessary:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.21.8/UIApplication.m:1707 
2017-11-09 16:57:05.233169+0000 MyARApp[2573:1099927] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'accessing _cachedSystemAnimationFence requires the main thread' 
*** First throw call stack: 
(0x186f51d04 0x1861a0528 0x186f51bd8 0x1878e1c24 0x1905e9c4c 0x19064646c 0x190432338 0x19038fe5c 0x1903fb6a8 0x190470bf0 0x1906ffb00 0x190701434 0x190703cd8 0x19070420c 0x190703c28 0x190467ab4 0x190707ae4 0x190b38854 0x190ca6b30 0x190ca69d4 0x1906f7e18 0x1020a27cc 0x19a66c610 0x19a725a84 0x19a723e00 0x19a724d1c 0x19a5a0cc4 0x19a6717ac 0x19a671b14 0x19a671f8c 0x19a71a67c 0x19a5d24a0 0x19a6e1c90 0x1035b949c 0x1035b945c 0x1035c8110 0x1035bc9a4 0x1035c9104 0x1035d0100 0x186b7afd0 0x186b7ac20) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 
+0

コンソールにエラーメッセージが表示されますか? – Larme

+0

コンソールに表示される完全なエラーは何ですか? – rmaddy

+0

@Larme質問を更新しました。 –

答えて

1

と様々な問題を修正、私はそれらを一つずつ取るよ:

> *** Assertion failure in -[UIApplication _cachedSystemAnimationFenceCreatingIfNecessary:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.21.8/UIApplication.m:1707 
> *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'accessing _cachedSystemAnimationFence requires the main thread' 

原因最初の問題だことクラッシュこれは、メインスレッドで呼び出される必要があるCocoaTouchの内部メソッドについて話しています。

問題はperformSegueWithIdentifier:sender:にあります。すべてのUI関連の呼び出しは、メインスレッドで行う必要があります。 はそれを修正するには:

dispatch_async(dispatch_get_main_queue(), ^(){ 
    [self performSegueWithIdentifier:@"brandSegue" sender:self]; 
}); 

が、これは第二の問題を明らかにした修正:二回

それはそれが引き金seguesを、これが起こることがあり、なぜあなたは知っていますか?

あなたはこれをやっている:

for (size_t i = 0; i < trackableCount; i++) 
{ 
    if (somethingTest) 
    { 
     [self performSegueWithIdentifier:@"brandSegue" sender:self]; 
    } 
} 

あなたのforループでは、有効な複数回somethingTestしていないことを言った誰ですか?

これを修正するには(ロジックについては、dispatch_async(dispatch_get_main_queue(){()}の部分をアルゴリズムに追加しないようにしました)。

//Declare a var before the for loop 
BOOL seguedNeedsToBeDone = FALSE; 
for (size_t i = 0; i < trackableCount; i++) 
{ 
    if (somethingTest) 
    { 
     seguedNeedsToBeDone = TRUE; 
    } 
} 
//Perform the segue after the for loop if needed 
if (seguedNeedsToBeDone) 
{ 
    [self performSegueWithIdentifier:@"brandSegue" sender:self]; 
} 

次号、先のViewControllerにデータを渡す:

JSONViewController *destViewController = segue.destinationViewController; 
destViewController.brand = @"something"; 

あなたはスウィフト&を混合しているのObjective-Cは、XCodeのはJSONViewControllerオブジェクトのプロパティであることbrandを知らない不満しているので、あなたが必要varの宣言の前に@objcを追加します。より詳細な答えはhereです。私はより安全な方法があると思い

//Calling the performSegue with custom value to pass 
[self performSegueWithIdentifier:@"brandSegue" sender:someVarToSend]; 

//Passing the custom value 
destViewController.brand = someVarToSend; 
0

最後に、送信者が使用しているループのためにあなたのデータを渡すために先端が(それは別のVARなどを作成するよりも、コードの期間中に高速です) seguesを使用するのではなく、navigationController?.pushViewController(_:animated :)を使用します。

関連する問題