Permalink
Browse files

Initial commit

  • Loading branch information...
mxcl committed Sep 5, 2011
1 parent 27da95b commit ac4a25ff3ccdef84562fcdc71f7d4485465c196a
Showing with 252 additions and 0 deletions.
  1. +6 −0 MBActionSheet.h
  2. +21 −0 MBActionSheet.m
  3. +8 −0 MBCategories.h
  4. +8 −0 MBCategories.m
  5. +14 −0 NSArray+mxcl.h
  6. +32 −0 NSArray+mxcl.m
  7. +5 −0 NSString+mxcl.h
  8. +7 −0 NSString+mxcl.m
  9. +7 −0 NSThread+mxcl.h
  10. +27 −0 NSThread+mxcl.m
  11. +5 −0 NSURLConnection+mxcl.h
  12. +53 −0 NSURLConnection+mxcl.m
  13. +22 −0 README
  14. +11 −0 UIColor+mxcl.h
  15. +11 −0 UIColor+mxcl.m
  16. +5 −0 UITableView+mxcl.h
  17. +10 −0 UITableView+mxcl.m
View
@@ -0,0 +1,6 @@
+#import <UIKit/UIKit.h>
+
+@interface MBActionSheet : UIActionSheet <UIActionSheetDelegate>
++ (id)actionSheet;
+@property (nonatomic, copy) void (^completionBlock)(NSUInteger index);
+@end
View
@@ -0,0 +1,21 @@
+#import "MBActionSheet.h"
+
+@implementation MBActionSheet
+@synthesize completionBlock;
+
++ (id)actionSheet {
+ MBActionSheet *sheet = [MBActionSheet new];
+ sheet.delegate = sheet;
+ return [sheet autorelease];
+}
+
+- (void)dealloc {
+ [completionBlock release];
+ [super dealloc];
+}
+
+- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
+ completionBlock(buttonIndex);
+ self.completionBlock = nil;
+}
+@end
View
@@ -0,0 +1,8 @@
+#import "MBActionSheet.h"
+#import "NSArray+mxcl.h"
+#import "NSString+mxcl.h"
+#import "NSThread+mxcl.H"
+#import "NSURLConnection+mxcl.h"
+#import "UIAlertView+mxcl.h"
+#import "UIColor+mxcl.h"
+#import "UITableView+mxcl.h"
View
@@ -0,0 +1,8 @@
+#import "MBActionSheet.m"
+#import "NSArray+mxcl.m"
+#import "NSString+mxcl.m"
+#import "NSThread+mxcl.m"
+#import "NSURLConnection+mxcl.m"
+#import "UIAlertView+mxcl.m"
+#import "UIColor+mxcl.m"
+#import "UITableView+mxcl.m"
View
@@ -0,0 +1,14 @@
+#import <Foundation/Foundation.h>
+
+@interface NSArray (RubyEnumerable)
+- (id)map:(id (^)(id o))block;
+@end
+
+@interface NSArray (mxcl)
+- (id)sortedArrayUsingDescriptor:(NSSortDescriptor *)descriptor;
+- (id)firstObject;
+@end
+
+@interface NSMutableArray (mxcl)
+- (void)sortUsingDescriptor:(NSSortDescriptor *)descriptor;
+@end
View
@@ -0,0 +1,32 @@
+#import "NSArray+mxcl.h"
+
+@implementation NSArray (RubyEnumerable)
+- (NSArray *)map:(id (^)(id obj))block {
+ NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
+ for (id item in self) {
+ id o = block(item);
+ if (o)
+ [array addObject:o];
+ }
+ return array;
+}
+@end
+
+
+@implementation NSArray (mxcl)
+- (id)sortedArrayUsingDescriptor:(NSSortDescriptor *)descriptor {
+ return [self sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
+}
+- (id)firstObject {
+ return self.count > 0
+ ? [self objectAtIndex:0]
+ : nil;
+}
+@end
+
+
+@implementation NSMutableArray (mxcl)
+- (void)sortUsingDescriptor:(NSSortDescriptor *)descriptor {
+ [self sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
+}
+@end
View
@@ -0,0 +1,5 @@
+#import <Foundation/Foundation.h>
+
+@interface NSString (mxcl)
+- (BOOL)matches:(NSString *)pattern;
+@end
View
@@ -0,0 +1,7 @@
+#import "NSString+mxcl.h"
+
+@implementation NSString (mxcl)
+- (BOOL)matches:(NSString *)pattern {
+ return [self rangeOfString:pattern options:NSRegularExpressionSearch].location != NSNotFound;
+}
+@end
View
@@ -0,0 +1,7 @@
+#import <Foundation/Foundation.h>
+
+@interface NSThread (BlocksAdditions)
+- (void)performBlock:(void (^)())block;
+- (void)performBlock:(void (^)())block waitUntilDone:(BOOL)wait;
++ (void)performBlockInBackground:(void (^)())block;
+@end
View
@@ -0,0 +1,27 @@
+#import "NSThread+mxcl.h"
+
+@implementation NSThread (BlocksAdditions)
+- (void)performBlock:(void (^)())block
+{
+ if ([[NSThread currentThread] isEqual:self])
+ block();
+ else
+ [self performBlock:block waitUntilDone:NO];
+}
+- (void)performBlock:(void (^)())block waitUntilDone:(BOOL)wait
+{
+ [NSThread performSelector:@selector(ng_runBlock:)
+ onThread:self
+ withObject:[[block copy] autorelease]
+ waitUntilDone:wait];
+}
++ (void)ng_runBlock:(void (^)())block
+{
+ block();
+}
++ (void)performBlockInBackground:(void (^)())block
+{
+ [NSThread performSelectorInBackground:@selector(ng_runBlock:)
+ withObject:[[block copy] autorelease]];
+}
+@end
View
@@ -0,0 +1,5 @@
+#import <Foundation/Foundation.h>
+
+@interface NSURLConnection (Blocks)
++ (id)connectionWithRequest:(id)rq twohundred:(void (^)(NSData *))block200 epicfail:(void (^)(NSError *))badblock;
+@end
View
@@ -0,0 +1,53 @@
+#import "NSURLConnection+mxcl.h"
+
+@interface MBURLConnection : NSURLConnection {
+ NSMutableData *data;
+ int rc;
+}
+@property (nonatomic, copy) void (^block200)(NSData *);
+@property (nonatomic, copy) void (^badblock)(NSError *);
+@end
+
+
+@implementation MBURLConnection
+@synthesize badblock, block200;
+- (id)initWithRequest:(id)rq {
+ self = [super initWithRequest:rq delegate:self];
+ if (self) {
+ data = [NSMutableData new];
+ }
+ return self;
+}
+- (void)dealloc {
+ [data release];
+ [badblock release];
+ [block200 release];
+ [super dealloc];
+}
+- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
+ if (rc == 200) {
+ block200(data);
+ } else {
+ badblock(nil);
+ }
+}
+- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
+ badblock(error);
+}
+- (void)connection:(id)connection didReceiveData:(id)newdata {
+ [data appendData:newdata];
+}
+- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
+ rc = response.statusCode;
+}
+@end
+
+
+@implementation NSURLConnection (Blocks)
++ (id)connectionWithRequest:(id)rq twohundred:(void (^)(NSData *))block200 epicfail:(void (^)(NSError *))badblock {
+ MBURLConnection *conn = [[MBURLConnection alloc] initWithRequest:rq];
+ conn.badblock = badblock;
+ conn.block200 = block200;
+ return [conn autorelease];
+}
+@end
View
22 README
@@ -0,0 +1,22 @@
+Suggested usage:
+
+* git submodule init
+ git submodule add https://github.com/mxcl/MBCategories
+
+* Add the single file MBCategories.m to your project.
+* #import "MBCategories.h" in your precompiled header.
+
+This way you don't have to add or remove files to your project when keeping
+MBCategories up-to-date.
+
+If anyone knows a good way to drop an XcodeProj file as a sub-project into an
+Xcode 4 iOS project so that it builds all the sources and all just works,
+please let me know.
+
+______________________________________________________________________________
+I, Max Howell the copyright holder of this work, hereby release it into the
+public domain. This applies worldwide.
+
+In case this is not legally possible, I grant any entity the right to use this
+work for any purpose, without any conditions, unless such conditions are
+required by law.
View
@@ -0,0 +1,11 @@
+#import <UIKit/UIKit.h>
+
+@interface UIColor (mxcl)
++ (id)randomColor;
+@end
+
+#define UIColorFromRGB(rgbValue) \
+ [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
+ green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
+ blue:((float)(rgbValue & 0xFF))/255.0 \
+ alpha:1.0]
View
@@ -0,0 +1,11 @@
+#import "UIColor+mxcl.h"
+
+@implementation UIColor (mxcl)
++(UIColor *)randomColor {
+// srandom(time(NULL));
+ CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX;
+ CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
+ CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
+ return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
+}
+@end
View
@@ -0,0 +1,5 @@
+#import <UIKit/UIKit.h>
+
+@interface UITableView (mxcl)
+- (UITableViewCell *)dequeueReusableCell;
+@end
View
@@ -0,0 +1,10 @@
+#import "UITableView+mxcl.h"
+
+@implementation UITableView (mxcl)
+- (UITableViewCell *)dequeueReusableCell {
+ #define CELLID @"mxcl_UITableViewCellID"
+ return [self dequeueReusableCellWithIdentifier:CELLID]
+ ?: [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELLID] autorelease];
+ #undef CELLID
+}
+@end

0 comments on commit ac4a25f

Please sign in to comment.