Skip to content

Instantly share code, notes, and snippets.

View RuiAAPeres's full-sized avatar
๐Ÿ’ญ
๐Ÿ”๐Ÿƒโ€โ™‚๏ธ

Rui Peres RuiAAPeres

๐Ÿ’ญ
๐Ÿ”๐Ÿƒโ€โ™‚๏ธ
View GitHub Profile
@RuiAAPeres
RuiAAPeres / gist:11190505
Created April 22, 2014 18:59
Swizzling valueForKey: and objectForKey:
#import <objc/runtime.h>
@implementation NSDictionary (Swizzled)
static void swizzInstance(Class class, SEL originalSel, SEL newSel)
{
Method origMethod = class_getInstanceMethod(class, originalSel);
Method newMethod = class_getInstanceMethod(class, newSel);
method_exchangeImplementations(origMethod, newMethod);
@RuiAAPeres
RuiAAPeres / gist:8905610
Last active August 29, 2015 13:56
RACSignal + Filter + Timer
// The goal is to make a server call every 5 minutes, but I want to make sure the user has at least moved 500 meters, how would go after this:
[RACObserve(self, userLocation) filter:^BOOL(CLLocation *newLocation)
{
return [newLocation distanceFromLocation:self.userLocation]>APH500Meters;
}];
// I was thinking on creating a scheduler so I got this:
RACScheduler *scheduler = [RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault];
@RuiAAPeres
RuiAAPeres / gist:8458663
Created January 16, 2014 16:56
Animating a layer with transition to the right + changing the contents
CAAnimationGroup *animationsGroup = [CAAnimationGroup animation];
CATransition *t = [CATransition animation];
t.type = kCATransitionPush;
t.subtype = kCATransitionFromRight;
t.duration = 1.0f;
id currentContents = _layer.contents;
_layer.contents = (id)([UIImage imageNamed:@"image2.jpg"].CGImage);
@RuiAAPeres
RuiAAPeres / gist:7656683
Last active December 29, 2015 10:19
The parseData is the private method, where the parsing actually works (NSData => NSDictionary). The other one parseLoginData (the public method), builds the model object from the Dictionary.
+ (void)parseLoginData:(NSData *)responseData withCompletionBlock:(MJDataSourceCompletionBlock)completionBlock
{
[self parseData:responseData withCompletionBlock:^(id response, MJError *error)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
MJLoginResponse *loginResponse = [[MJLoginResponse alloc] initWithLoginResponse:response];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
void SwizzleClassMethod(Class c, SEL orig, SEL new) {
Method origMethod = class_getClassMethod(c, orig);
Method newMethod = class_getClassMethod(c, new);
c = object_getClass((id)c);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
@RuiAAPeres
RuiAAPeres / gist:7379148
Last active December 27, 2015 19:39
Returning a Class that complies to a given protocol
Class sportsFeedManager = (Class <RPSportsBoundryProtocol>)[RPInteractor sportsFeedManager];
[sportsFeedManager yahooSportsFeedWithCompletion:^(NSArray *sportsFeeds, NSError *error)
{
if (error)
{
// TODO: Handle the error;
}
else
{
@RuiAAPeres
RuiAAPeres / gist:7159304
Created October 25, 2013 18:15
WillShow
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}
@RuiAAPeres
RuiAAPeres / NSObject+FacebookImage
Created August 29, 2013 21:06
Just a small piece of code to get the basic info + profile picture
+ (void(^)())facebookImageBlockWithCompletionHandler:(void(^)(id profilePicture, id userInformation, NSError *error))completionHandler
{
void(^facebookBlock)()= ^{[FBSession openActiveSessionWithReadPermissions:@[@"basic_info", @"email"]
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error)
{
if (!error)
{