Here's an example of key-value validation.
According to Apple:
Key-value coding provides a consistent API for validating a property value. The validation infrastructure provides a class the opportunity to accept a value, provide an alternate value, or deny the new value for a property and give a reason for the error.
https://developer.apple.com/library/mac/documentation/cocoa/conceptual/KeyValueCoding/Articles/Validation.html
Method Signature
-(BOOL)validateName:(id *)ioValue error:(NSError * __autoreleasing *)outError {
// Implementation specific code.
return ...;
}
Properly calling the method
Apple:
You can call validation methods directly, or by invoking validateValue:forKey:error: and specifying the key.
Ours:
//Shows random use of this
-(void)myRandomMethod{
NSError *error;
BOOL validCreditCard = [self validateCreditCard:myCreditCard error:error];
}
Our test implementation of your request
//Validate credit card
-(BOOL)validateCreditCard:(id *)ioValue error:(NSError * )outError{
Card *card = (Card*)ioValue;
//Validate different parts
BOOL validNumber = [self validateCardNumber:card.number error:outError];
BOOL validExpiration = [self validateExpiration:card.expiration error:outError];
BOOL validSecurityCode = [self validateSecurityCode:card.securityCode error:outError];
//If all are valid, success
if (validNumber && validExpiration && validSecurityCode) {
return YES;
//No success
}else{
return NO;
}
}
-(BOOL)validateExpiration:(id *)ioValue error:(NSError * )outError{
BOOL isExpired = false;
//Implement expiration
return isExpired;
}
-(BOOL)validateSecurityCode:(id *)ioValue error:(NSError * )outError{
//card security code should not be nil and more than 3 characters long
if ((*ioValue == nil) || ([(NSString *)*ioValue length] < 3)) {
//Make sure error us not null
if (outError != NULL) {
//Localized string
NSString *errorString = NSLocalizedString(
@"A card's security code must be at least three digits long",
@"validation: Card, too short expiration error");
//Place into dict so we can add it to the error
NSDictionary *userInfoDict = @{ NSLocalizedDescriptionKey : errorString };
//Error
*outError = [[NSError alloc] initWithDomain:CARD_ERROR_DOMAIN
code:CARD_INVALID_SECURITY_CODE
userInfo:userInfoDict];
}
return NO;
}
return YES;
}
-(BOOL)validateCardNumber:(id *)ioValue error:(NSError * )outError{
BOOL isValid = false;
//Implement card number verification
return isValid;
}