Skip to content

Instantly share code, notes, and snippets.

View lukeredpath's full-sized avatar
🏠
Working from home

Luke Redpath lukeredpath

🏠
Working from home
View GitHub Profile
@lukeredpath
lukeredpath / UITableView+FixConstraints.h
Created July 18, 2013 15:07
Replaces constraints created in a XIB/Storyboard on a UITableViewCell that relate subviews of the contentView with the cell itself, with ones that relate the subviews with the contentView, as it should be. This bug is fixed in Xcode 5 DP3, apparently.
@interface UITableViewCell (FixConstraints)
/* Finds any constraints that relate between a subview of the cell's content
* view and the cell itself, and replaces them with otherwise identical constraints
* that replace the cell with the content view.
*
* This works around a bug in Interface Builder where subviews added to a content
* view have their constraints related to the cell itself and not the content view,
* which can cause problems if you need to move the content view in any way (e.g.
* swipe to delete).
On the basis that you are the intended recipient, then receipt of this email by you represents your confirmation that you agree, or continue to agree, to be bound by our standard Terms and Conditions applicable to the transaction to which this email relates. If you require a copy (or a replacement copy) of the applicable standard Terms and Conditions please email your request by return
I have an entity Foo, which has a 1-n relationship with Bar. Bar has a 1-n relationship with Baz.
On the main thread, the UI is showing a list of all the Bars in a single Foo. I am using KVO on Foo's bars relationship to detect when Bars are added to Foo.
Using a child MOC of the main MOC (the UI uses the main MOC), I create a new Bar, associate it with a couple of Baz objects, then add it to Foo's bars relationship.
Then I save the child MOC, which causes its changes to be merged into the main MOC.
At this point, my KVO observer on foo.bars fires, and when I inspect the change dictionary, I see my inserted Bar object however, the Bar's bazes relationship is empty!
NSManagedObjectContext *contextOne = ...;
NSArray *objects = ...<result of fetch request on contextOne...
NSManagedObjectID *objectID = objects[0].objectID;
NSManagedObjectContext *contextTwo = ...;
id object = [contextTwo existingObjectWithID:objectID error:nil]; // returns the right object
NSAssert(![object.objectID isTemporaryID]); // passes
id otherObject = [contextTwo objectWithID:objectID]; // always returns an object, should be the right object
NSAssert(![otherObject.objectID isTemporaryID]); // fails!
@lukeredpath
lukeredpath / interest_calculator.rb
Last active December 14, 2015 06:29
Find the lowest possible monthly payment to pay off balance in full with compound interest, recursive solution.
EPISILON = 0.01 # calculate to the nearest cent
def calculate_balance_remaining_after_year(starting_balance, monthly_payment, annual_interest_rate)
12.times.reduce(starting_balance) do |balance|
balance -= monthly_payment
balance += (annual_interest_rate / 12) * balance
end
end
def calculate_lowest_payment_within_bounds(starting_balance, annual_interest_rate, lower_bound, upper_bound)
@lukeredpath
lukeredpath / NSLayoutConstraintBuilder.mm
Created January 31, 2013 16:44
An alternative higher level API over NSLayoutConstraint, NOT inspired by ASCII art.
NSLayoutConstraintBuilder *builder = [NSLayoutConstraintBuilder builder];
UIView *mySubview = [[UIView alloc] initWithFrame:CGRectZero];
// API deliberately mirrors the terminology of IB:
[builder pin:mySubview toWidth:200];
[builder pin:mySubview toHeight:200];
[builder pin:mySubview toSize:CGSizeMake(200, 200)];
/* Allows you to give a context a descriptive name to aid in
* debugging.
*/
@interface NSManagedObjectContext (DescriptiveName)
@property (nonatomic, copy) NSString *descriptiveName;
@end
NSCalendar *utcCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSISO8601Calendar];
[utcCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
State A (current):
branch foo: A -- B -- C -- D
\
branch bar: -- E -- F -- G --H
I want commit E from bar brought back into foo, without cherry-picking and creating a new commit. Essentially I want to go to this:
State B:
Pod::Spec.new do |s|
s.name = 'LRMocky'
s.version = '1.0'
s.license = 'MIT'
s.summary = 'A mock object library for Objective C, inspired by JMock 2.0'
s.homepage = 'http://github.com/lukeredpath/LRMocky'
s.authors = { 'Luke Redpath' => '[email protected]' }
s.source = { :git => 'https://github.com/lukeredpath/LRMocky.git' }
s.requires_arc = true