Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

UIView optimization, UIControl setTarget: action: forControlEvents:, NSNumber, UIColor minor fix #121

Merged
merged 2 commits into from

1 participant

Sam Soffes
Deleted user
  • UIColor colorWithHex: method accepts 0x along with # as a valid prefix
  • UIControl setTarget: action: forControlEvents: method allows to set exclusive handler removing all previously registered actions
  • Performance optimization UIView firstSuperviewOfClass: method
  • NSNumber category
    • dateValue method creates an instance of NSDate with current number as a timestamp
    • pluralWithForms: or pluralWithForms: language: method returns correct plural form for the current number, e.g. 1 tweet vs 2 tweets
Alexander Zats + `UIColor` `colorWithHex:` method accepts `0x` along with `#` as a
valid prefix
+ `UIControl` `setTarget: action: forControlEvents:` method allows to
set exclusive handler removing all previously registered actions
+ Performance optimization `UIView` `firstSuperviewOfClass:` method
+ `NSNumber` category
	+ `dateValue` method creates an instance of `NSDate` with current
number as a timestamp
	+ `pluralWithForms:` or `pluralWithForms: language:` method returns
correct plural form for the current number, e.g. `1 tweet` vs `2 tweets`
7610eea
Sam Soffes
Owner

Totally love everything here except the plural stuff. TTTLocalizedPluralString by @mattt is a much more complete solution. I'd let people use his fantastic library instead of starting to add support in SSToolkit since it is such a large problem.

Can you send me a pull request without the plural stuff?

Sam Soffes soffes merged commit 8110ac9 into from
Sam Soffes
Owner

Thanks so much! Really great additions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 23, 2012
  1. + `UIColor` `colorWithHex:` method accepts `0x` along with `#` as a

    Alexander Zats authored
    valid prefix
    + `UIControl` `setTarget: action: forControlEvents:` method allows to
    set exclusive handler removing all previously registered actions
    + Performance optimization `UIView` `firstSuperviewOfClass:` method
    + `NSNumber` category
    	+ `dateValue` method creates an instance of `NSDate` with current
    number as a timestamp
    	+ `pluralWithForms:` or `pluralWithForms: language:` method returns
    correct plural form for the current number, e.g. `1 tweet` vs `2 tweets`
Commits on May 26, 2012
This page is out of date. Refresh to see the latest.
12 SSToolkit.xcodeproj/project.pbxproj
View
@@ -7,6 +7,9 @@
objects = {
/* Begin PBXBuildFile section */
+ 962F5F5E156CCD1200408648 /* NSNumber+SSToolkitAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 962F5F5B156CCD1200408648 /* NSNumber+SSToolkitAdditions.h */; };
+ 962F5F5F156CCD1200408648 /* NSNumber+SSToolkitAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 962F5F5C156CCD1200408648 /* NSNumber+SSToolkitAdditions.m */; };
+ 962F5F60156CCD1200408648 /* UIColor+SSToolkitAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 962F5F5D156CCD1200408648 /* UIColor+SSToolkitAdditions.m */; };
9EB278B114BBC4360077194C /* SSConcurrentOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = B24E9E11121DC29A0085F81E /* SSConcurrentOperation.m */; };
AACBBE4A0F95108600F1A2B1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACBBE490F95108600F1A2B1 /* Foundation.framework */; };
B2136B3C13ABCC0C00FEBCFD /* SSBorderedView.h in Headers */ = {isa = PBXBuildFile; fileRef = B2136B3A13ABCC0C00FEBCFD /* SSBorderedView.h */; };
@@ -167,6 +170,9 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ 962F5F5B156CCD1200408648 /* NSNumber+SSToolkitAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSNumber+SSToolkitAdditions.h"; sourceTree = "<group>"; };
+ 962F5F5C156CCD1200408648 /* NSNumber+SSToolkitAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSNumber+SSToolkitAdditions.m"; sourceTree = "<group>"; };
+ 962F5F5D156CCD1200408648 /* UIColor+SSToolkitAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIColor+SSToolkitAdditions.m"; sourceTree = "<group>"; };
AACBBE490F95108600F1A2B1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
B2136B3A13ABCC0C00FEBCFD /* SSBorderedView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SSBorderedView.h; sourceTree = "<group>"; };
B2136B3B13ABCC0C00FEBCFD /* SSBorderedView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SSBorderedView.m; sourceTree = "<group>"; };
@@ -483,6 +489,9 @@
B2601A711223079A005506D5 /* Foundation */ = {
isa = PBXGroup;
children = (
+ 962F5F5B156CCD1200408648 /* NSNumber+SSToolkitAdditions.h */,
+ 962F5F5C156CCD1200408648 /* NSNumber+SSToolkitAdditions.m */,
+ 962F5F5D156CCD1200408648 /* UIColor+SSToolkitAdditions.m */,
B24E9E05121DC29A0085F81E /* NSArray+SSToolkitAdditions.h */,
B24E9E06121DC29A0085F81E /* NSArray+SSToolkitAdditions.m */,
B24E9E07121DC29A0085F81E /* NSData+SSToolkitAdditions.h */,
@@ -726,6 +735,7 @@
B2C50814151BAB4000E94614 /* NSBundle+SSToolkitAdditions.h in Headers */,
B2F72A131534D54C00556B66 /* SSRateLimit.h in Headers */,
B2C7ED6C153A5624006ABE73 /* SSButton.h in Headers */,
+ 962F5F5E156CCD1200408648 /* NSNumber+SSToolkitAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -952,6 +962,8 @@
B2C50815151BAB4000E94614 /* NSBundle+SSToolkitAdditions.m in Sources */,
B2F72A141534D54C00556B66 /* SSRateLimit.m in Sources */,
B2C7ED6D153A5624006ABE73 /* SSButton.m in Sources */,
+ 962F5F5F156CCD1200408648 /* NSNumber+SSToolkitAdditions.m in Sources */,
+ 962F5F60156CCD1200408648 /* UIColor+SSToolkitAdditions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
23 SSToolkit/NSNumber+SSToolkitAdditions.h
View
@@ -0,0 +1,23 @@
+//
+// NSNumber+SSToolkitAdditions.h
+// SSToolkit
+//
+// Created by Alexander Zats on 5/22/12.
+// Copyright (c) 2012 Sam Soffes. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSNumber (SSToolkitAdditions)
+
+///--------------
+/// @name Date from timestamp
+///--------------
+
+/**
+ Creates an instance of `NSDate` using current number as timestamp.
+ @return NSDate with current number as unix timestamp or `nil` if current number contains 0.
+ */
+- (NSDate *)dateValue;
+
+@end
22 SSToolkit/NSNumber+SSToolkitAdditions.m
View
@@ -0,0 +1,22 @@
+//
+// NSNumber+SSToolkitAdditions.m
+// SSToolkit
+//
+// Created by Alexander Zats on 5/22/12.
+// Copyright (c) 2012 Sam Soffes. All rights reserved.
+//
+
+#import "NSNumber+SSToolkitAdditions.h"
+
+@implementation NSNumber (SSToolkitAdditions)
+
+- (NSDate *)dateValue
+{
+ NSTimeInterval timestamp = [self doubleValue];
+ if (!timestamp) {
+ return nil;
+ }
+ return [NSDate dateWithTimeIntervalSince1970:timestamp];
+}
+
+@end
6 SSToolkit/UIColor+SSToolkitAdditions.m
View
@@ -23,9 +23,11 @@ - (NSUInteger)_hexValue {
@implementation UIColor (SSToolkitAdditions)
+ (UIColor *)colorWithHex:(NSString *)hex {
- // Remove `#`
- if ([[hex substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"#"]) {
+ // Remove `#` and `0x`
+ if ([hex hasPrefix:@"#"]) {
hex = [hex substringFromIndex:1];
+ } else if ([hex hasPrefix:@"0x"]) {
+ hex = [hex substringFromIndex:2];
}
// Invalid if not 3, 6, or 8 characters
5 SSToolkit/UIControl+SSToolkitAdditions.h
View
@@ -16,4 +16,9 @@
*/
- (void)removeAllTargets;
+/**
+ Sets exclusive target for specified event, all previous targets will be removed, usefull for table cells etc
+ */
+- (void)setTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
+
@end
12 SSToolkit/UIControl+SSToolkitAdditions.m
View
@@ -16,4 +16,16 @@ - (void)removeAllTargets {
}];
}
+- (void)setTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
+{
+ NSSet *targets = [self allTargets];
+ for (id currentTarget in targets) {
+ NSArray *actions = [self actionsForTarget:currentTarget forControlEvent:controlEvents];
+ for (NSString *currentAction in actions) {
+ [self removeTarget:currentTarget action:NSSelectorFromString(currentAction) forControlEvents:controlEvents];
+ }
+ }
+ [self addTarget:target action:action forControlEvents:controlEvents];
+}
+
@end
11 SSToolkit/UIView+SSToolkitAdditions.m
View
@@ -74,16 +74,11 @@ - (NSArray *)superviews {
return superviews;
}
-
- (id)firstSuperviewOfClass:(Class)superviewClass {
- UIView *view = self;
- UIView *superview = nil;
- while (view) {
- superview = [view superview];
- if ([superview isKindOfClass:superviewClass]) {
- return superview;
+ for (UIView *view = [self superview]; view != nil; view = [view superview]) {
+ if ([view isKindOfClass:superviewClass]) {
+ return view;
}
- view = superview;
}
return nil;
}
Something went wrong with that request. Please try again.