如何处理 iOS4.0 的 iPhone 模拟器 NSInspiration 问题+

发布于 2024-09-27 14:18:12 字数 1522 浏览 4 评论 0 原文

对于iPhone Simulator iOS4.0+,NSInitation 不能很好地处理异常。我遇到了 解决方法。当我尝试将其作为 objc_msgSend(target_, [调用选择器]) 进行以下调用并在 createResponseFromIn Vocation() 下注释掉 [调用调用] 时,挂起。我尝试了不同的方法来调用 obj_msgSend 但没有成功。

 
- (NSInvocation *)createInvocationWithSelector:(SEL)selector
                                     signature:(NSMethodSignature *)method
                                     arguments:(NSDictionary *)arguments {
  NSInvocation *invocation
    = [NSInvocation invocationWithMethodSignature:method];
  [invocation setSelector:selector];
  [invocation setTarget:target_];

  if (arguments != nil) {
    if ([method numberOfArguments] > 2) {
      [invocation setArgument:&arguments atIndex:2];
    }
  }

  return invocation;
}

// Invoke the given invocation and create a response from it.
- (WDResponse *)createResponseFromInvocation:(NSInvocation *)invocation {
  WDResponse *response;

  @try {
    [invocation invoke];
    if ([[invocation methodSignature] methodReturnLength] == 0) {
      response = [WDResponse responseWithValue:nil];
    } else {
      id result;
      [invocation getReturnValue:&result];
      response = [WDResponse responseWithValue:result];
    }
  }
  @catch (NSException * e) {
    NSLog(@"Method invocation error: %@", e);
    response = [WDResponse responseWithError:e];
  }
  return response;
}

For iPhone Simulator iOS4.0+, NSInvocation doesn't handle exceptions well. I came across a workaround to use objc_msgSend. When I tried it for the below invocation as objc_msgSend(target_, [invocation selector]) and commenting out [invocation invoke] under createResponseFromInvocation() hangs. I tried different ways of calling obj_msgSend and didn't work.

 
- (NSInvocation *)createInvocationWithSelector:(SEL)selector
                                     signature:(NSMethodSignature *)method
                                     arguments:(NSDictionary *)arguments {
  NSInvocation *invocation
    = [NSInvocation invocationWithMethodSignature:method];
  [invocation setSelector:selector];
  [invocation setTarget:target_];

  if (arguments != nil) {
    if ([method numberOfArguments] > 2) {
      [invocation setArgument:&arguments atIndex:2];
    }
  }

  return invocation;
}

// Invoke the given invocation and create a response from it.
- (WDResponse *)createResponseFromInvocation:(NSInvocation *)invocation {
  WDResponse *response;

  @try {
    [invocation invoke];
    if ([[invocation methodSignature] methodReturnLength] == 0) {
      response = [WDResponse responseWithValue:nil];
    } else {
      id result;
      [invocation getReturnValue:&result];
      response = [WDResponse responseWithValue:result];
    }
  }
  @catch (NSException * e) {
    NSLog(@"Method invocation error: %@", e);
    response = [WDResponse responseWithError:e];
  }
  return response;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自←快乐 2024-10-04 14:18:12

我找到了一个解决方案,完全删除 NSInitation 并在选择器上调用 objc_msgSend 。此解决方法有助于解决所有 NSInitation 异常处理。

I found a solution by removing NSInvocation completely and calling objc_msgSend on the selector. This workaround helped in resolving all NSInvocation Exception handling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文