iOS assertion is better than nslog

Source: Internet
Author: User

Sometimes we need a constant output to debug the program, asserting that it is seldom used by people. Look at the code written by a great God online today

Nsassert (!gmenu, @ "Singleton object");

Nsparameterassert (view);

We checked the document below.

#define NSPARAMETERASSERT (condition) Nsassert ((condition), @ "Invalid parameter not satisfying:%s", #condition)

#if!defined (_nsassertbody) #define NSASSERT (condition, desc, ...) Do {__pragma_push_no_extra_arg_warnings if (!) ( condition) {[[Nsassertionhandler Currenthandler] Handlefailureinmethod:_cmd object:self file:[nsstring STRINGWITHUTF8STRING:__FILE__] linenumber:__line__ Description: (DESC), # #__VA_ARGS__]; } __pragma_pop_no_extra_arg_warnings} while (0) #endif

The essence is still nsassert.

The first argument is a condition, and if False, throws an exception, showing the information described by the second argument.

As an example:

Nsinteger a = 5;    Nsinteger B = 0;    Nsinteger c = 0; Assert Nsassert (b!=0, @ "B is 0");

Only under Debug, it causes crash and displays in the system:

2014-07-23 13:43:33.465 catchcrash[8747:60b] *** terminating app due to  uncaught exception  ' nsinternalinconsistencyexception ', reason:  ' b  is   0 ' * * *  first throw call stack: (0   corefoundation                        0X0000000101947495 __EXCEPTIONPREPROCESS + 1651   LIBOBJC. a.dylib                      0x00000001016a699e objc_exception_throw + 432    corefoundation                       0x000000010194731a +[nsexception raise:format:arguments:] +  1063   Foundation                           0x0000000101242f19 -[nsassertionhandler handlefailureinmethod:object: file:linenumber:description:] + 1894   catchcrash                            0x0000000100001850 -[ViewController viewDidLoad] + 10565    uikit                                0x000000010034959e -[ uiviewcontroller loadviewifrequired] + 5626   uikit                                 0x0000000100349777 -[uiviewcontroller view] + 297    UIKit                                0x000000010028996b  -[UIWindow addRootViewControllerViewIfPossible] + 588   UIKit                                 0x0000000100289c70 -[uiwindow _ sethidden:forced:] + 2829   uikit                                 0x0000000111039d73 -[uiwindowaccessibility (SafeCategory)  _ orderfrontwithoutmakingkey] + 6810  uikit                                 0x0000000100292ffa -[uiwindow makekeyandvisible] + 5111  uikit                                 0x000000010024ec98 -[uiapplication _ callinitializationdelegatesforurl:payload:suspended:] + 178812  uikit                                 0x0000000100252a0c -[uiapplication _ runwithurl:payload:launchorientation:statusbarstyle:statusbarhidden:] + 66013  uikit                                 0x0000000100263d4c -[uiapplication handleevent:withnewevent:] +  318914  UIKit                                 0x0000000100264216 -[uiapplication sendevent:] + 7915  uikit                                 0x0000000100254086 _uiapplicationhandleevent  + 57816  GraphicsServices                     0x0000000103ac071a _PurpleEventCallback  + 76217  graphicsservices                     0x0000000103ac01e1 purpleeventcallback + 3518  corefoundation                        0x00000001018c9679 __cfrunloop_is_calling_out_to_a_source1_perform_function__  + 4119  CoreFoundation                       0x00000001018c944e __cfrunloopdosource1  + 47820  CoreFoundation                       0x00000001018f2903 __CFRunLoopRun  + 193921  corefoundation                        0x00000001018f1d83 cfrunlooprunspecific + 46722   UIKit                                 0x00000001002522e1 -[uiapplication _run] + 60923  uikit                                 0x0000000100253e33 UIApplicationMain +  101024  catchcrash                           0x0000000100001c47 main  + 10325  libdyld.dylib                        0x0000000101fdf5fd start + 126  ???                                   0x0000000000000001  0X0 + 1) libc++abi.dylib: terminating with uncaught exception of  Type nsexception (LLDB)


The first line of the code area above is missing some strings and is re-written below. is to write the reason for crash.

* * * * terminating app due to uncaught exception ' nsinternalinconsistencyexception ', Reason: ' B is a 0 '

First throw Call stack:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.