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).
NSAssert vs NSCAssert
-
11-10-2022 - |
Вопрос
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
.
Решение
Другие советы
Note that using NSAssert/NSParameterAssert within a block may cause a retain cycle. Use the C variants instead. Google NSAssert retain cycle
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 useNSParameterAssert/NSCParameterAssert
statements at the top of methods to enforce any preconditions about the input values; in all other cases, useNSAssert/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).
NSAssert should only be used in an Objective-C context and NSCAssert should only be used in a C context (i.e. functions).