试图从Objective-C转向Swift VC [英] Attempting to segue from Objective-C to Swift VC

查看:205
本文介绍了试图从Objective-C转向Swift VC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从objective-c转换为swift。

i'm trying to segue from objective-c to swift.

但是当我使用下面的对象尝试此操作时,我收到以下错误

However when I try this using the object below I receive the following error

我不知道我做错了什么,我在故事板上设置segue并将其分配给相同的ID,创建准备功能和执行。

I don't know what i'm doing wrong, i've setup the segue on the storyboard and assigned it to the same ID, created the prepare function and the perform.

- (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;
  }
}

编辑 - 错误信息

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) 


推荐答案

在聊天讨论并解决各种问题后,我会逐一介绍:

After discussion in Chat and fixing various issues, I'll take them one by one:

> *** 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的内部方法。

That's the first issue causing a crash. This is talking about an internal method of CocoaTouch needed to be called in main thread.

问题在于你的 performSegueWithIdentifier:sender:。所有与UI相关的调用都必须在主线程中完成。
修复它:

The issue lies on your performSegueWithIdentifier:sender:. All UI related calls have to be done in main thread. To fix it:

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

修复这个问题揭示了第二个问题:

Fixing this revealed a second issue:


它会分段,但它会触发两次,你知道为什么会发生这种情况吗?

it segues but it trigger twice, do you know why this may happen?

你这样做了:

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

谁说你的for循环你没有多次有效 somethingTest

Who said that in your for loop you don't valid multiple times somethingTest?

修复它(我说的是逻辑,我没做 dispatch_async(dispatch_get_main_queue(){()} 部分,以避免在算法中添加噪音。)

To fix it (I'm talking about the logic, I didn't do the dispatch_async(dispatch_get_main_queue(){()} part to avoid adding noise to the algorithm).

//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];
}

下一期,将数据传递给Destination ViewController:

Next issue, passing data to the Destination ViewController:

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

你正在混合Swift& Objective-C,因为XCode抱怨不知道品牌 JSONViewController 对象的属性,你需要添加<在声明var之前code> @objc 。可以找到更详细的答案这里

You are mixing Swift & Objective-C, since XCode was complaining about not knowing brand being a property of JSONViewController object, you needed to add @objc before the declaration of the var. More detailed answer can be found here.

最后,传递你的循环数据的提示是使用发送者(它在编码方面比创建另一个var更快,等等。):

Finally, a tip to pass the data of you for loop is using the sender (it's faster in term of coding than creating another var, etc.):

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

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

这篇关于试图从Objective-C转向Swift VC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆