使用NSSetUncaughtExceptionHandler目标C注册的UncaughtExceptionHandler [英] Register UncaughtExceptionHandler in Objective C using NSSetUncaughtExceptionHandler

查看:156
本文介绍了使用NSSetUncaughtExceptionHandler目标C注册的UncaughtExceptionHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code使用注册未捕获的异常处理程序的UncaughtExceptionHandler 是如下,你认为会有什么潜在的问题?

My code for registering uncaught exception handler using UncaughtExceptionHandler is as following, do you think there will be any potential issue?

@interface AppDelegate ()

void myHandler(NSException * exception);

@end

@implementation AppDelegate

void myHandler(NSException * exception)
{
  // ...
}

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSSetUncaughtExceptionHandler(&myHandler);
..

是否有可能有写它一个更简洁的方式?

Is it possible to have a more concise way to write it?

我需要使用类扩展,以宣布原型摆脱否previous原型功能..

I need to use the class extension to declare prototype in order to get rid of the warning of No previous prototype for function ..

推荐答案

马丁的答案是正确的。不过,我想我只是阐述了一下,解释为什么它是如此。

Martin's answer is correct. However, I thought I'd elaborate just a bit, to explain why it is so.

您函数定义:

void myHandler(NSException * exception)
{
  // ...
}

定义,这将是外部可见的功能。在其他(广义的,非技术)字样,象征将目标文件中创建,使连接器能够找到它,它允许其他文件来调用将myHandler

但是,因为它被认为是外部可见的,其他文件都将不得不知道函数是什么样子。这就是原型的用武之地。警告基本上是说......

However, because it is supposed to be externally visible, other files are going to have to know what that function looks like. That's where the prototype comes into play. The warning is basically saying...

嘿,您已声明此功能是其他外部可见
  code,但我没有看到一个原型,其他code可以用它来知道
  有关功能。

Hey, you have declared this function to be externally visible to other code, but I don't see a prototype that other code can use to know about the function.

所以,你会得到一个警告。这是一个很好的警示,以对。它可以帮助你记住,对于要导出的函数声明原型。

So, you get a warning. It's a good warning to have on. It helps you remember to declare prototypes for functions that you want to export.

现在,当你发现,你可以声明原型,并警告消失。然而,仅仅在实现文件中声明原型应该是一个警告你。那个人警告应该是:

Now, as you discovered, you can declare a prototype, and the warning goes away. However, declaring the prototype just in the implementation file should be another warning to you. That personal warning should be:

你真的想要这个功能有外部的知名度,还是仅仅叫这个编译单元?如果函数是不会有外部的可视性,那么就没有必要将其输出在符号表中,并没有必要为一个原型其他模块可以包括使他们了解功能

Do you really want this function to have external visibility, or is it just called in this compilation unit? If the function is not going to have external visibility, then there is no need to export it in the symbol table, and there is no need for a prototype that other modules can include so they know about the function.

在这种情况下,你可以声明函数静态在马丁的回答是:

In that case, you can declare the function static as in Martin's response:

static void myHandler(NSException * exception)
{
  // ...
}

在此背景下,静态告诉编译器是这样的:

In this context, static tells the compiler something like:

嘿,编译器,创建code此功能,并允许任何code
  这个编译单元看到的功能,但不给它的外部
  能见度。我不希望的功能被其他模块调用。

Hey, compiler, create code for this function, and allow any code in this compilation unit to see the function, but do not give it external visibility. I don't want the function to be called by other modules.

在这种情况下,即使其他code申报的原型,他们不会看你的功能,因为它是私人到其所定义的文件。

In this case, even if other code declared the prototype, they would not see your function because it is "private" to the file in which it is defined.

因为它是被仅用于在本地文件,没有必要为一个原型,所以没有必要警告您不必之一。

Since it is being used only in the local file, there is no need for a prototype, so there is no need to warn you that you don't have one.

现在,就像一记...你并不需要把C函数在code的@interface和@implementation部分,如什么也不做。这些C函数编译使用完全相同的可见性和访问它们是否ObjC段内或没有。

Now, just as a note... You don't need to put C functions in the @interface and @implementation sections of your code, as that does nothing. Those C functions are compiled with the exact same visibility and access whether they are inside the ObjC sections or not.

最后,如果你愿意,你可以禁用在X code构建设置这样的警告(不过现在你了解警告的情况下,我建议离开它)。

Finally, if you want, you can disable this warning in the Xcode build settings (but now that you understand the context of the warning, I suggest leaving it on).

这篇关于使用NSSetUncaughtExceptionHandler目标C注册的UncaughtExceptionHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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