开发者

What is NSParameterAssert?

开发者 https://www.devze.com 2022-12-24 13:26 出处:网络
What is NS开发者_运维问答ParameterAssert? Can anyone explain with example?It is a simple way to test that a method\'s parameter is not nil or not 0. So basically, you use it to create a precondition

What is NS开发者_运维问答ParameterAssert?

Can anyone explain with example?


It is a simple way to test that a method's parameter is not nil or not 0. So basically, you use it to create a precondition, stating that some parameter must be set. If it is not set, the macro causes the application to abort and generates an error on that line. So:

- (void)someMethod:(id)someObjectThatMustNotBeNil
{
  // Make sure that someObjectThatMustNotBeNil is really not nil
  NSParameterAssert( someObjectThatMustNotBeNil );
  // Okay, now do things
}

Pre-conditions are a simple way to ensure that methods/API are being called correctly by the programmer. The idea is that if a programmer violates the precondition, the application terminates early--hopefully during debugging and basic testing.

NSParameterAssert can be used to test that any expression evaluates to be true, however, so you can use it like this as well:

NSParameterAssert( index >= 0 ); // ensure no negative index is supplied

Apple's documentation for the NSParameterAssert() macro

0

精彩评论

暂无评论...
验证码 换一张
取 消