Created
September 21, 2016 19:54
-
-
Save IainDelaney/0eedb6cd342b73cc0fc533b0bf9bd92b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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