<bdo id='VrmeU'></bdo><ul id='VrmeU'></ul>

  • <i id='VrmeU'><tr id='VrmeU'><dt id='VrmeU'><q id='VrmeU'><span id='VrmeU'><b id='VrmeU'><form id='VrmeU'><ins id='VrmeU'></ins><ul id='VrmeU'></ul><sub id='VrmeU'></sub></form><legend id='VrmeU'></legend><bdo id='VrmeU'><pre id='VrmeU'><center id='VrmeU'></center></pre></bdo></b><th id='VrmeU'></th></span></q></dt></tr></i><div id='VrmeU'><tfoot id='VrmeU'></tfoot><dl id='VrmeU'><fieldset id='VrmeU'></fieldset></dl></div>
    1. <legend id='VrmeU'><style id='VrmeU'><dir id='VrmeU'><q id='VrmeU'></q></dir></style></legend>
    2. <small id='VrmeU'></small><noframes id='VrmeU'>

    3. <tfoot id='VrmeU'></tfoot>

        如何在我的 iPhone 应用程序中使用 NSError?

        How can I use NSError in my iPhone App?(如何在我的 iPhone 应用程序中使用 NSError?)
          <tbody id='7cpd8'></tbody>

        <small id='7cpd8'></small><noframes id='7cpd8'>

      1. <tfoot id='7cpd8'></tfoot>

        <i id='7cpd8'><tr id='7cpd8'><dt id='7cpd8'><q id='7cpd8'><span id='7cpd8'><b id='7cpd8'><form id='7cpd8'><ins id='7cpd8'></ins><ul id='7cpd8'></ul><sub id='7cpd8'></sub></form><legend id='7cpd8'></legend><bdo id='7cpd8'><pre id='7cpd8'><center id='7cpd8'></center></pre></bdo></b><th id='7cpd8'></th></span></q></dt></tr></i><div id='7cpd8'><tfoot id='7cpd8'></tfoot><dl id='7cpd8'><fieldset id='7cpd8'></fieldset></dl></div>

                <bdo id='7cpd8'></bdo><ul id='7cpd8'></ul>

                  <legend id='7cpd8'><style id='7cpd8'><dir id='7cpd8'><q id='7cpd8'></q></dir></style></legend>
                • 本文介绍了如何在我的 iPhone 应用程序中使用 NSError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在处理我的应用程序中的错误,我正在研究使用 NSError.我对如何使用它以及如何填充它感到有些困惑.

                  I am working on catching errors in my app, and I am looking into using NSError. I am slightly confused about how to use it, and how to populate it.

                  有人可以提供一个关于我如何填充然后使用 NSError 的示例吗?

                  Could someone provide an example on how I populate then use NSError?

                  推荐答案

                  好吧,我通常做的是让我的可能在运行时出错的方法引用 NSError 指针.如果该方法确实出了问题,我可以使用错误数据填充 NSError 引用并从该方法返回 nil.

                  Well, what I usually do is have my methods that could error-out at runtime take a reference to a NSError pointer. If something does indeed go wrong in that method, I can populate the NSError reference with error data and return nil from the method.

                  例子:

                  - (id) endWorldHunger:(id)largeAmountsOfMonies error:(NSError**)error {
                      // begin feeding the world's children...
                      // it's all going well until....
                      if (ohNoImOutOfMonies) {
                          // sad, we can't solve world hunger, but we can let people know what went wrong!
                          // init dictionary to be used to populate error object
                          NSMutableDictionary* details = [NSMutableDictionary dictionary];
                          [details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];
                          // populate the error object with the details
                          *error = [NSError errorWithDomain:@"world" code:200 userInfo:details];
                          // we couldn't feed the world's children...return nil..sniffle...sniffle
                          return nil;
                      }
                      // wohoo! We fed the world's children. The world is now in lots of debt. But who cares? 
                      return YES;
                  }
                  

                  然后我们可以使用这样的方法.除非方法返回 nil,否则不要费心检查错误对象:

                  We can then use the method like this. Don't even bother to inspect the error object unless the method returns nil:

                  // initialize NSError object
                  NSError* error = nil;
                  // try to feed the world
                  id yayOrNay = [self endWorldHunger:smallAmountsOfMonies error:&error];
                  if (!yayOrNay) {
                     // inspect error
                     NSLog(@"%@", [error localizedDescription]);
                  }
                  // otherwise the world has been fed. Wow, your code must rock.
                  

                  我们能够访问错误的 localizedDescription,因为我们为 NSLocalizedDescriptionKey 设置了一个值.

                  We were able to access the error's localizedDescription because we set a value for NSLocalizedDescriptionKey.

                  了解更多信息的最佳位置是 Apple 的文档.确实不错.

                  The best place for more information is Apple's documentation. It really is good.

                  可可是我的女朋友.

                  这篇关于如何在我的 iPhone 应用程序中使用 NSError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What values should I use for iOS boolean states?(我应该为 iOS 布尔状态使用什么值?)
                  Why does Objective-C use YES/NO macro convention instead of true/false?(为什么 Objective-C 使用 YES/NO 宏约定而不是 true/false?)
                  How do I get NSJSONSerialization to output a boolean as true or false?(如何让 NSJSONSerialization 将布尔值输出为真或假?)
                  Is there any difference between bool, Boolean, and BOOL in Objective-C?(Objective-C 中的 bool、Boolean 和 BOOL 之间有什么区别吗?)
                  BOOL to NSString(布尔到 NSString)
                  Set bool property of all objects in the array(设置数组中所有对象的布尔属性)
                    <tfoot id='QjRJg'></tfoot>
                    <i id='QjRJg'><tr id='QjRJg'><dt id='QjRJg'><q id='QjRJg'><span id='QjRJg'><b id='QjRJg'><form id='QjRJg'><ins id='QjRJg'></ins><ul id='QjRJg'></ul><sub id='QjRJg'></sub></form><legend id='QjRJg'></legend><bdo id='QjRJg'><pre id='QjRJg'><center id='QjRJg'></center></pre></bdo></b><th id='QjRJg'></th></span></q></dt></tr></i><div id='QjRJg'><tfoot id='QjRJg'></tfoot><dl id='QjRJg'><fieldset id='QjRJg'></fieldset></dl></div>
                  1. <legend id='QjRJg'><style id='QjRJg'><dir id='QjRJg'><q id='QjRJg'></q></dir></style></legend>
                      <bdo id='QjRJg'></bdo><ul id='QjRJg'></ul>

                        <tbody id='QjRJg'></tbody>
                    • <small id='QjRJg'></small><noframes id='QjRJg'>