ios - Objective C Validation -
i've been watching wwdc 2014 advanced ios application architecture , patterns , found interesting , suited case. specially validation part. in example app i've check if team build way. checks be:
- more 4 players
- two female, 2 male
- etc
so i've created far:
@protocol dexvalidator <nsobject> @required - ( bool ) validate:(nserror * __autoreleasing *)error; @end //implements procotol dexvalidator @implementation dexvalidatorhasfourplayers - ( bool ) validate:(nserror *__autoreleasing *)error { if( _team.length != 4 ) *error = .... return _team.length == 4 } @end
so far can see how works, came part didn't understand. talks composition , creates , overall validator checks this:
//implements procotol dexvalidator @implementation dexvalidatorteam - ( instancetype ) init { ... _hasfourplayers = [dexvalidatorhasfourplayers new]; ...morechecks... ... } - ( bool ) validate:(nserror *__autoreleasing *)error { ... }
but question how can return nserrors caller. can display list wrong team. since in video changes display of form elements. or should overall not implement validator protocol , else?
i didnt see video yet, might have misunderstood question. anyway, wouldn't possible use pattern return pointer nsarray
containing nserror
s, , not single nserror
? this:
- (bool)validate:(nsarray *__autoreleasing *)errors { nsmutablearray *temperrors = [nsmutablearray new]; if (team.length != 4) { [temperrors addobject:[nserror errorwithdomain:@"123" code:123 userinfo:nil]]; } if (!team.isgenderequal) { [temperrors addobject:[nserror errorwithdomain:@"456" code:456 userinfo:nil]]; } if (temperrors.count > 0) { *errors = [nsarray arraywitharray:temperrors]; } return temperrors.count == 0; }
you call by:
nsarray *errors; bool success = [self validate:&errors]; (nserror *error in errors) { nslog(@"error %@", error); }
Comments
Post a Comment