objective c - Enforcing to supply a completion block -
i'm wondering whether or not practice:
i have method takes in parameters , callback block, let's along lines of:
-(void)loginwithusername:(nsstring *)username andpassword:(nsstring *)password withcompletion:(loginmanagercompletionblock)completionhandler;
now in specific case, there no use in calling method without completion handler, triggers redundant call login web service (also, not change state of - not client side nor server side). avoid these situations actively enforcing requirement of passing me completion block in order make web service call. sort of think of "@required" method in objective c protocol. questions are:
- is requiring completion block in order perform action practice in objective c?
- (edit: answered) how enforce requirement? there built-in language syntax can me out here?
thanks
you can use function attribute nonnull(params)
, params 1 or more comma-separated parameter numbers, indicate parameter should not null (nonnull
without parentheses means pointer parameters should not null). example:
- (void) loginwithusername:(nsstring *)username andpassword:(nsstring *)password withcompletion:(loginmanagercompletionblock)completionhandler __attribute__((nonnull(3)));
however, while compile time check , produces warning if null passed directly. if argument value expression evaluates null, e.g. variable null value, not caught.
if parameter being null error can add runtime check within method using nsparameterassert(parameter)
, parameter name of 1 of method's parameters, check condition. call defined print error message , throw exception if argument evaluates false, , null evaluates false.
Comments
Post a Comment