zoukankan      html  css  js  c++  java
  • 创建自己的NSError对象

    Generating Your Own Errors
    In order to create your own NSError objects you’ll need to define your own error domain, which should be of the form:

    com.companyName.appOrFrameworkName.ErrorDomain
    You’ll also need to pick a unique error code for each error that may occur in your domain, along with a suitable description, which is stored in the user info dictionary for the error, like this:

        NSString *domain = @"com.MyCompany.MyApplication.ErrorDomain";
        NSString *desc = NSLocalizedString(@"Unable to…", @"");
        NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
     
        NSError *error = [NSError errorWithDomain:domain
                                             code:-101
                                         userInfo:userInfo];
    This example uses the NSLocalizedString function to look up a localized version of the error description from a Localizable.strings file, as described in Localizing String Resources.

    If you need to pass back an error by reference as described earlier, your method signature should include a parameter for a pointer to a pointer to an NSError object. You should also use the return value to indicate success or failure, like this:

    - (BOOL)doSomethingThatMayGenerateAnError:(NSError **)errorPtr;
    If an error occurs, you should start by checking whether a non-NULL pointer was provided for the error parameter before you attempt to dereference it to set the error, before returning NO to indicate failure, like this:

    - (BOOL)doSomethingThatMayGenerateAnError:(NSError **)errorPtr {
        ...
        // error occurred
        if (errorPtr) {
            *errorPtr = [NSError errorWithDomain:...
                                            code:...
                                        userInfo:...];
        }
        return NO;
    }

  • 相关阅读:
    akka并发编程练习
    使用selenium和chromedriver组合爬虫时,如果爬取的页面数量巨多,会出现占用内存逐渐增大知道程序崩溃的情况
    网易2017秋招编程题集合_以下代码全部来自牛客网
    牛客网_运行问题
    json和xml之间转换的好文
    Eclipse 启动时闪退问题解决方案
    关于opencv的文件配置详细内容
    第一个opencv声称图片_以及遇到的问题集锦
    好文收藏_关于find_first_not_of函数
    好文收藏readtxt_cpp
  • 原文地址:https://www.cnblogs.com/NSNULL/p/4482644.html
Copyright © 2011-2022 走看看