This is a collection of snippets that I use in my everyday code sessions. (Its content is updated thanks to Kilianc's Indexer https://github.com/kilianc/yari-gists-indexer)
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
- (Entry*)entryForDate:(NSDate *)date inContext:(NSManagedObjectContext*)context{ | |
NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Entry"]; | |
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"since >= %@",date]; | |
request.predicate = predicate; | |
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"since" ascending:YES]; | |
request.sortDescriptors = @[sort]; | |
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
- (NSArray*)monthsNames{ | |
if (!_monthsNames) { | |
NSDateFormatter * df = [[NSDateFormatter alloc] init]; | |
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]]; | |
_monthsNames = [df monthSymbols]; | |
} | |
return _monthsNames; | |
} |
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
Entry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"Entry" inManagedObjectContext:self.managedObjectContext]; | |
entry.amount = [NSNumber numberWithFloat:[self.UI_amountTextField.text floatValue]]; | |
[self.managedObjectContext save:&error]; |
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
+ (NSDate*)dateAddingMonths:(NSInteger)months{ | |
NSDateComponents* dateComponents = [[NSDateComponents alloc]init]; | |
[dateComponents setMonth:months]; | |
NSCalendar* calendar = [NSCalendar currentCalendar]; | |
NSDate* newDate = [calendar dateByAddingComponents:dateComponents toDate:self options:0]; //NSDate category | |
return newDate; | |
} |
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
+ (AnythingManager*)sharedAnythingManager{ | |
static AnythingManager *anythingManager; | |
static dispatch_once_t token; | |
dispatch_once(&token, ^{ | |
anythingManager = [[AnythingManager alloc]init]; | |
}); | |
return anythingManager; |
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
NSString * L(NSString * translation_key, NSString * lang) { | |
NSString * s = NSLocalizedString(translation_key, nil); | |
// Force translation as "Lang" language if no translations are found | |
if ([s isEqualToString:translation_key]) { | |
NSString * path = [[NSBundle mainBundle] pathForResource:lang ofType:@"lproj"]; | |
NSBundle * languageBundle = [NSBundle bundleWithPath:path]; | |
s = [languageBundle localizedStringForKey:translation_key value:@"" table:nil]; | |
} |
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
// This assumes we have an NSManagedObject with the NSDate* date property | |
- (void)setDate:(NSDate *)date{ | |
[self willChangeValueForKey:@"date"]; | |
[self setPrimitiveValue:date forKey:@"date"]; | |
[self didChangeValueForKey:@"date"]; | |
//If needed | |
//[self updateOtherData]; | |
} |
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
@dynamic theProperty; //The property should set as dynamic | |
- (id)initWithLayer:(id)layer { | |
if (self = [super initWithLayer:layer]) { | |
if ([layer isKindOfClass:[PieMask class]]) { | |
YourLayer *currentPresentation = (YourLayer *)layer; | |
self.theProperty = currentPresentation.theProperty; |
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
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"]; | |
anim.fromValue = @(0.0); | |
anim.toValue = @(1.0); | |
CGPoint point = [self createPosition] | |
CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:@"position"]; | |
anim2.fromValue = [NSValue valueWithCGPoint:label.position]; | |
anim2.toValue = [NSValue valueWithCGPoint:point]; | |
CAAnimationGroup *group = [CAAnimationGroup animation]; |