菜鸟教程小白 发表于 2022-12-11 20:15:02

iphone - 更早地检测有问题的 XIB View


                                            <p><p>我的 nib 名称中有一个拼写错误,后来在我推送到导航 Controller 时它在代码中爆炸了。弄清楚它并没有花太长时间,但我认为最好早点断言格式良好,以便更容易弄清楚。问题是它不是零,它只是不能从 Nib 正确地形成。在 initWithNib 之后是否有更好的断言或捕获来检查代码中的早期问题? </p>

<pre><code>//
// typo in nib name - I want to catch before it blows up inside of pushViewController to narrow the problem
//
ENPurchaseDetailView *purchaseView = [
    initWithNibName:@&#34;XibWithTypoInIt&#34; bundle:nil];
assert(purchaseView != nil);// does not catch - it&#39;s not nil - just not well formed

// other code ...

// blows up with sigabort inside of pushViewController
[ pushViewController:purchaseView animated:YES];   
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>创建一个基础 UIViewController 类,您可以从该类继承所有其他 VC。然后,您可以在基类中执行许多有用的断言,而不必将它们分散在整个代码中。这是我使用的一个</p>

<pre><code>- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
   self =

// never do this stuff in production code
#ifdef DEBUG
    // make sure you didn&#39;t fat finger Xib name
    if (nibNameOrNil)
    {
      NSString *path = [ pathForResource:nibNameOrNil ofType:@&#34;nib&#34;];
      NSAssert(path, @&#34;Nib %@ does not exist&#34;, nibNameOrNil);
    }

    // make sure there&#39;s no problems with the Xib
    UINib *nib = ;
    NSArray *instantiatedObjects = ;
    NSAssert(instantiatedObjects &amp;&amp; &gt; 0, @&#34;Not well formed Xib&#34;);
#endif       return self;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 更早地检测有问题的 XIBView ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/7486373/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/7486373/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 更早地检测有问题的 XIB View