What is NSAssert1?

怎甘沉沦 提交于 2019-12-23 08:46:44

问题


I am developing an application on iOS. I see there is a macro called NSAssert1. What is it for? What are the differences in usage between NSLog and NSAssert1?

Please guide me or suggest a tutorial where I can read about it.


回答1:


NSAssert variants take a condition and a message. If the condition isn't met/true, then the assertion fails and NSAssert raises an exception with the message provided. For example, NSAssert((a == b), @"Error message"); will raise an exception when a is not equal to b. NSAssert1 is a variant that takes an additional argument and inserts it into the format string provided, like so: NSAssert1((a == b), @"Error message: %@", someErrorString);

NSLog will just write something to the console.

Documentation for all of those macros is on Apple's developer site.




回答2:


NSAssert, NSParameterAssert, NSAssert1, and friends are assertion macros. Assertions are condition checks that scream when something is not right:

- (void) doSomethingWithPointer: (Foo*) foo
{
    NSAssert(foo != NULL, @"The Foo pointer must not be NULL!");
    foo->something;
}

See questions tagged “assertions” here on Stack Overflow for more information.



来源:https://stackoverflow.com/questions/5496378/what-is-nsassert1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!