Skip to content

Commit

Permalink
Some code optimizations, minor fixes, and documentation corrections.
Browse files Browse the repository at this point in the history
• Changed PMValidationUnit to use fast enumeration in the validateText method instead of the block form. Performance comparisons bear out that fast enumeration is the better choice for most reasonable use cases, and avoids concurrency issues.
• Changed all class constructors to use return instancetype instead of id.
• Fixed some NSInteger/NSUInteger mismatches.
• Updated comments to reflect this and some previous changes.
  • Loading branch information
poetmountain committed Mar 24, 2014
1 parent 872f05d commit 65a0741
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Classes/PMValidationManager.h
Expand Up @@ -26,7 +26,7 @@
/**
Returns an auto-released instance of this class
*/
+ (PMValidationManager *)validationManager;
+ (instancetype)validationManager;


///----------------------------------
Expand Down
4 changes: 2 additions & 2 deletions Classes/PMValidationManager.m
Expand Up @@ -46,7 +46,7 @@ -(id)init {


// returns a new instance of PMValidationManager
+ (PMValidationManager *) validationManager {
+ (instancetype) validationManager {

PMValidationManager *vm = [[PMValidationManager alloc] init];

Expand Down Expand Up @@ -89,7 +89,7 @@ -(PMValidationUnit *)registerObject:(id)object forValidationTypes:(NSOrderedSet
NSString *unit_identifier = identifier;
if (unit_identifier == nil) {
// if no identifier passed in, create one
unit_identifier = [[NSNumber numberWithInteger:[self.validationUnits count]+1] stringValue];
unit_identifier = [[NSNumber numberWithUnsignedInteger:[self.validationUnits count]+1] stringValue];
}

// create validation unit with passed-in types and save it
Expand Down
4 changes: 2 additions & 2 deletions Classes/PMValidationUnit.h
Expand Up @@ -26,7 +26,7 @@
/**
Returns an auto-released instance of this class
*/
+ (PMValidationUnit *) validationUnit;
+ (instancetype) validationUnit;


///--------------------------------------
Expand Down Expand Up @@ -76,7 +76,7 @@
/**
Finds a `PMValidationType` associated with a provided identifier
@param identifier The string associated with the `PMValidationType` which was provided during the initWithValidationTypes or registerValidationType methods.
@param theIdentifier The string associated with the `PMValidationType` which was provided during the initWithValidationTypes or registerValidationType methods.
@return The `PMValidationType` instance, or nil if none was found for the provided identifier.
Expand Down
14 changes: 6 additions & 8 deletions Classes/PMValidationUnit.m
Expand Up @@ -85,7 +85,7 @@ -(id)initWithValidationTypes:(NSOrderedSet *)validationTypes identifier:(NSStrin


// returns a new instance of PMValidationUnit
+ (PMValidationUnit *) validationUnit {
+ (instancetype) validationUnit {

PMValidationUnit *vu = [[PMValidationUnit alloc] init];

Expand Down Expand Up @@ -160,15 +160,13 @@ - (void)validateText:(NSString *)text {
if (weak_self) {
__strong PMValidationUnit *strong_self = weak_self;
if (strong_self) {
__block NSInteger num_valid = 0;
[strong_self.registeredValidationTypes enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(PMValidationType *type,NSUInteger idx, BOOL *stop) {

NSUInteger num_valid = 0;
for (PMValidationType *type in strong_self.registeredValidationTypes) {
BOOL is_valid = [type isTextValid:text];
num_valid += [[NSNumber numberWithBool:is_valid] integerValue];

}];
num_valid += [[NSNumber numberWithBool:is_valid] unsignedIntegerValue];
}

NSInteger type_count = [strong_self.registeredValidationTypes count];
NSUInteger type_count = [strong_self.registeredValidationTypes count];
(num_valid == type_count) ? (strong_self.isValid = YES) : (strong_self.isValid = NO);

strong_self.lastTextValue = text;
Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationEmailType.h
Expand Up @@ -24,7 +24,7 @@
@discussion All subclasses should override this method to return an instance of the subclass.
*/
+ (id) validator;
+ (instancetype) validator;


///---------------------
Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationEmailType.m
Expand Up @@ -12,7 +12,7 @@ @implementation PMValidationEmailType


// returns a new instance of PMValidationEmailType
+ (id) validator {
+ (instancetype) validator {

PMValidationEmailType *val = [[PMValidationEmailType alloc] init];

Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationLengthType.h
Expand Up @@ -26,7 +26,7 @@
@discussion All subclasses should override this method to return an instance of the subclass.
*/
+ (id) validator;
+ (instancetype) validator;


///---------------------
Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationLengthType.m
Expand Up @@ -28,7 +28,7 @@ -(id)init {


// returns a new instance of PMValidationLengthType
+ (id) validator {
+ (instancetype) validator {

PMValidationLengthType *val = [[PMValidationLengthType alloc] init];

Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationRegexType.h
Expand Up @@ -25,7 +25,7 @@
@discussion All subclasses should override this method to return an instance of the subclass.
*/
+ (id) validator;
+ (instancetype) validator;


///---------------------
Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationRegexType.m
Expand Up @@ -28,7 +28,7 @@ -(id)init {


// returns a new instance of PMValidationRegexType
+ (id) validator {
+ (instancetype) validator {

PMValidationRegexType *val = [[PMValidationRegexType alloc] init];

Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationStringCompareType.h
Expand Up @@ -25,7 +25,7 @@
@discussion All subclasses should override this method to return an instance of the subclass.
*/
+ (id) validator;
+ (instancetype) validator;


///---------------------
Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationStringCompareType.m
Expand Up @@ -50,7 +50,7 @@ -(BOOL) isTextValid:(NSString *)text {
#pragma mark - Class methods

// returns a new instance of PMValidationStringMatchType
+ (id) validator {
+ (instancetype) validator {

PMValidationStringCompareType *val = [[PMValidationStringCompareType alloc] init];

Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationType.h
Expand Up @@ -29,7 +29,7 @@
@discussion All subclasses of PMValidationType should override this method to return an instance of the subclass.
*/
+ (id) validator;
+ (instancetype) validator;


///---------------------------
Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationType.m
Expand Up @@ -32,7 +32,7 @@ -(id)init {


// returns a new instance of PMValidationType
+ (id)validator {
+ (instancetype)validator {

PMValidationType *val = [[PMValidationType alloc] init];

Expand Down
4 changes: 2 additions & 2 deletions Classes/ValidationTypes/PMValidationUITextCompareType.h
Expand Up @@ -28,7 +28,7 @@
@discussion All subclasses should override this method to return an instance of the subclass.
*/
+ (id) validator;
+ (instancetype) validator;


///---------------------
Expand Down Expand Up @@ -65,7 +65,7 @@
/**
Registers a `UITextView` instance whose text value should be used to compare the target validation string against.
@param textField The `UITextView` instance to listen to `UITextViewTextDidChangeNotification` notifications from.
@param textView The `UITextView` instance to listen to `UITextViewTextDidChangeNotification` notifications from.
*/
- (void) registerTextViewToMatch:(UITextView *)textView;
Expand Down
2 changes: 1 addition & 1 deletion Classes/ValidationTypes/PMValidationUITextCompareType.m
Expand Up @@ -93,7 +93,7 @@ - (void) textDidChangeNotification:(NSNotification *)notification {
#pragma mark - Class methods

// returns a new, autoreleased instance of PMValidationUITextMatchType
+ (id) validator {
+ (instancetype) validator {

PMValidationUITextCompareType *val = [[PMValidationUITextCompareType alloc] init];

Expand Down
51 changes: 29 additions & 22 deletions README.md
Expand Up @@ -31,16 +31,20 @@ length_type.maximumCharacters = 8;
PMValidationUnit *unit = [PMValidationUnit validationUnit];
[unit registerValidationType:length_type];

// listen for validation updates from unit
[[NSNotificationCenter defaultCenter] addObserverForName:PMValidationUnitUpdateNotification object:unit queue:nil usingBlock:
^(NSNotification *notification) {
PMValidationUnit *unit = (PMValidationUnit *)notification.object;

if (!unit.isValid) {
NSDictionary *errors = [notification.userInfo valueForKey:@"errors"];
}
}
];
// get validation status update
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(validationStatusNotificationHandler:) name:PMValidationUnitUpdateNotification object:unit];

// listen for validation status updates
- (void)validationStatusNotificationHandler:(NSNotification *)notification {

PMValidationUnit *unit = (PMValidationUnit *)notification.object;

if (!unit.isValid) {
NSDictionary *errors = [notification.userInfo valueForKey:@"errors"];
}

}


// validate the string
[unit validateText:@"Velvet Underground"];
Expand All @@ -56,18 +60,21 @@ PMValidationEmailType *email_type = [PMValidationEmailType validator];
PMValidationUnit *email_unit = [manager registerTextField:self.emailTextField
forValidationTypes:[NSSet setWithObjects:email_type, nil]
identifier:@"email"];
// listen for validation updates from the manager
[[NSNotificationCenter defaultCenter] addObserverForName:PMValidationStatusNotification object:manager queue:nil usingBlock:
^(NSNotification *notification) {
BOOL is_valid = [(NSNumber *)[notification.userInfo objectForKey:@"status"] boolValue];
if (!is_valid) {
NSDictionary *units = [notification.userInfo objectForKey:@"units"];
NSDictionary *email_dict = [units objectForKey:email_type.identifier];
NSDictionary *email_errors = [email_dict objectForKey:@"errors"];
}
}
];
// get validation status update
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(validationStatusNotificationHandler:) name:PMValidationStatusNotification object:self.validationManager];
- (void)validationStatusNotificationHandler:(NSNotification *)notification {
BOOL is_valid = [(NSNumber *)[notification.userInfo objectForKey:@"status"] boolValue];
if (!is_valid) {
NSDictionary *units = [notification.userInfo objectForKey:@"units"];
NSDictionary *email_dict = [units objectForKey:email_type.identifier];
NSDictionary *email_errors = [email_dict objectForKey:@"errors"];
}
}
```

### Targeting iOS 6 or greater
Expand Down

0 comments on commit 65a0741

Please sign in to comment.