Skip to content

Instantly share code, notes, and snippets.

View ariok's full-sized avatar
🧠
CodeLoving

Yari @bitwaker ariok

🧠
CodeLoving
View GitHub Profile
@ariok
ariok / [CoreData]CustomProperties.m
Created December 16, 2013 18:09
Setter method for NSManagedObject subclass properties.
// 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];
}
@ariok
ariok / INDEX.md
Created December 11, 2013 12:12
Testing the Gists Indexer by KilianC (https://github.com/kilianc/yari-gists-indexer)
@ariok
ariok / [pattern]Singleton.m
Created December 8, 2013 11:21
Basic Singleton pattern implementation
+ (AnythingManager*)sharedAnythingManager{
static AnythingManager *anythingManager;
static dispatch_once_t token;
dispatch_once(&token, ^{
anythingManager = [[AnythingManager alloc]init];
});
return anythingManager;
@ariok
ariok / [Date]AddingMonths.m
Created December 6, 2013 13:11
Adding some months to an existent date.//
+ (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;
}
@ariok
ariok / [CoreData]newobject.m
Created December 5, 2013 15:48
Add a new object in Core Data.
Entry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"Entry" inManagedObjectContext:self.managedObjectContext];
entry.amount = [NSNumber numberWithFloat:[self.UI_amountTextField.text floatValue]];
[self.managedObjectContext save:&error];
@ariok
ariok / [Date]MonthsNames.m
Last active December 30, 2015 03:29
Get an array with months names
- (NSArray*)monthsNames{
if (!_monthsNames) {
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];
_monthsNames = [df monthSymbols];
}
return _monthsNames;
}
@ariok
ariok / [CoreData]FetchPredicateSort.m
Created December 3, 2013 13:51
Fetching data with predicate and sorting
- (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];
@ariok
ariok / [UI]LoadWithXib.m
Created December 2, 2013 16:32
Loading the main view from a xib file
- (UIView *)loadWithNib {
NSArray *aNib = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *view = [aNib objectAtIndex:0];
return view;
}
@ariok
ariok / [Date]MonthName.m
Created November 29, 2013 18:18
Get the month name from its numeric representation
// 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;
}
@ariok
ariok / [Date]UnitForDate.m
Last active December 29, 2015 18:19
Return the integer representation of the required unit for the given date Note: this functions assumes that the current calendar is set to Gregorian and takes as units year, month,day, hour,minute and second.
// 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];