7

What is the difference between NSAssert and NSCAssert? When I explored their implementation they looked very similar and I couldn't find an answer to my question.

I guess that it will also explain the difference between NSParameterAssert and NSCParameterAssert.

KlimczakM
  • 12,576
  • 11
  • 64
  • 83

4 Answers4

13

The second is the difference between C and Objective-C assertions: NSAssert should only be used in an Objective-C context (i.e. method implementations), whereas NSCAssert should only be used in a C context (i.e. functions).

More at http://nshipster.com/nsassertionhandler/

Andrew
  • 3,166
  • 21
  • 32
6

Note that using NSAssert/NSParameterAssert within a block may cause a retain cycle. Use the C variants instead. Google NSAssert retain cycle

Mark Krenek
  • 4,889
  • 3
  • 25
  • 17
3

This is taken from NSHipster, see link

Foundation defines two pairs of assertion macros:

NSAssert/NSCAssert

NSParameterAssert/NSCParameterAssert

Foundation makes two distinctions in their assertion handler APIs that are both semantic and functional.

The first distinction is between a general assertion (NSAssert) and a parameter assertion (NSParameterAssert). As a rule of thumb, methods / functions should use NSParameterAssert/NSCParameterAssert statements at the top of methods to enforce any preconditions about the input values; in all other cases, use NSAssert/NSCAssert.

The second is the difference between C and Objective-C assertions: NSAssert should only be used in an Objective-C context (i.e. method implementations), whereas NSCAssert should only be used in a C context (i.e. functions).

DanSkeel
  • 3,853
  • 35
  • 54
Greg
  • 25,317
  • 6
  • 53
  • 62
2

NSAssert should only be used in an Objective-C context and NSCAssert should only be used in a C context (i.e. functions).

Sport
  • 8,570
  • 6
  • 46
  • 65