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 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 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 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 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 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 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 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
- (UIView *)loadWithNib { | |
NSArray *aNib = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:self options:nil]; | |
UIView *view = [aNib objectAtIndex:0]; | |
return view; | |
} |
This file contains 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
// Return the month name | |
+ (NSString*)monthName:(NSInteger)month{ | |
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
formatter.locale = [NSLocale currentLocale]; | |
NSString *monthName = [[formatter monthSymbols] objectAtIndex:(month - 1)]; | |
return monthName; | |
} |
This file contains 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
// Return integer representation of the required unit for the given date | |
// Note: It assumes that the current calendar is set to Gregorian | |
+ (NSInteger)unit:(NSCalendarUnit)unit forDate:(NSDate*)date{ | |
// Get the calendar | |
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; | |
[calendar setTimeZone:[NSTimeZone defaultTimeZone]]; | |
// Get the component | |
NSDateComponents *componets = [calendar components:unit fromDate:date]; |