Skip to content

Instantly share code, notes, and snippets.

@IainDelaney
Created September 21, 2016 19:54
Show Gist options
  • Save IainDelaney/0eedb6cd342b73cc0fc533b0bf9bd92b to your computer and use it in GitHub Desktop.
Save IainDelaney/0eedb6cd342b73cc0fc533b0bf9bd92b to your computer and use it in GitHub Desktop.
Note: clientId and facId are primary keys to the server database, numbers stored in NSStrings
// Mandatory fields: clientId, facId
- (NSError *)validateData {
id sel1 = [NSValue valueWithPointer:@selector(clientId)];
id sel2 = [NSValue valueWithPointer:@selector(facId)];
return [self validateMandatoryFields:@[sel1, sel2]];
}
- (NSError *)validateMandatoryFields:(NSArray *)fields {
NSMutableArray *nilFields = [NSMutableArray array];
for (id field in fields) {
SEL selector = [field pointerValue];
NSMethodSignature *method = [self methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:method];
[invocation setSelector:selector];
[invocation invokeWithTarget:self];
// We must use __unsafe_unretained to avoid decreasing retain count
id __unsafe_unretained value;
[invocation getReturnValue:&value];
if (!value) {
[nilFields addObject:NSStringFromSelector(selector)];
}
}
if (nilFields.lastObject) {
NSString *missingFields = [nilFields componentsJoinedByString:@", "];
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
userInfo[NSLocalizedFailureReasonErrorKey] = NSLocalizedString(@"Invalid Server Response", nil);
userInfo[NSLocalizedDescriptionKey] = NSLocalizedString(@"Please contact administrator and/or try again later.", nil);
PPLogError(kPELCAPIGlobal, @"VALIDATION ERROR: Model: %@, Fields: %@", NSStringFromClass([self class]), missingFields);
return [[NSError alloc] initWithDomain:kPPErrorDomainApp code:PPAppErrorInvalidData userInfo:userInfo];
} else {
return nil;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment