Skip to content

Instantly share code, notes, and snippets.

View matteom's full-sized avatar

Matteo Manferdini matteom

View GitHub Profile
@matteom
matteom / ViewModel.swift
Created March 11, 2018 13:09
Reversing view models in Swift
import Foundation
import UIKit
struct CreditCard {
let number: String
let expirationDate: Date
}
class CreditCardView {
@IBOutlet private weak var numberTextView: UITextView!
@matteom
matteom / gist:7549131
Last active December 28, 2015 19:19
Fetching the leads from core data to display in the table view
- (void)updateLeads {
NSFetchRequest *leadsFetchRequest = [NSFetchRequest fetchRequestWithEntityName:[PCLead entityName]];
leadsFetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"surname" ascending:YES]];
self.leads = [self.managedObjectContext executeFetchRequest:leadsFetchRequest error:NULL];
[self.tableView reloadData];
}
@matteom
matteom / gist:7549107
Created November 19, 2013 17:30
Configuring the managed object context and the delegate for the new controller
// In PCLeadsViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"DisplayLeadSegue"]) {
PCLeadViewController *leadViewController = segue.destinationViewController;
NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
leadViewController.lead = self.leads[selectedRowIndexPath.row];
} else {
PCNewLeadViewController *newLeadViewController = segue.destinationViewController;
newLeadViewController.managedObjectContext = self.managedObjectContext;
@matteom
matteom / gist:7549052
Last active December 28, 2015 19:19
View controller to create new leads
// Header file
@protocol PCNewLeadViewControllerDelegate <NSObject>
- (void)newLeadWasCreated;
@end
@interface PCNewLeadViewController : UIViewController
@matteom
matteom / gist:7548845
Created November 19, 2013 17:13
Creating new leads in Core Data
@implementation PCLead
@dynamic name;
@dynamic surname;
@dynamic company;
+ (instancetype)insertLeadInManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {
return [NSEntityDescription insertNewObjectForEntityForName:self.entityName inManagedObjectContext:managedObjectContext];
}
@matteom
matteom / gist:7548487
Created November 19, 2013 16:49
Initializing the core data stack in the app delegate
@interface PCAppDelegate ()
@property PCPersistentStack *persistentStack;
@end
@implementation PCAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.persistentStack = [[PCPersistentStack alloc] initWithStoreURL:[self storeURL] modelURL:[self modelURL]];
@matteom
matteom / gist:7548392
Last active December 28, 2015 19:09
Core data persistent stack
// Header file
@interface PCPersistentStack : NSObject
@property (readonly) NSManagedObjectContext *managedObjectContext;
- (instancetype)initWithStoreURL:(NSURL*)storeURL modelURL:(NSURL*)modelURL;
- (void)saveContext;
@end
@matteom
matteom / gist:7547296
Created November 19, 2013 15:41
Passing data from the table view controller to the lead view controller
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
PCLeadViewController *leadViewController = segue.destinationViewController;
NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
leadViewController.lead = self.leads[selectedRowIndexPath.row];
}
@matteom
matteom / gist:7546897
Created November 19, 2013 15:18
Leads table view controller populating the table view from a plist file
@interface PCLeadsViewController ()
@property NSArray *leads;
@end
@implementation PCLeadsViewController
- (void)viewDidLoad {
[super viewDidLoad];
@matteom
matteom / gist:7546844
Last active December 28, 2015 18:59
Table view cell for leads
@interface PCLeadCell ()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *companyLabel;
@end
@implementation PCLeadCell
- (void)setLead:(PCLead *)lead {