Skip to content

Instantly share code, notes, and snippets.

View ksm's full-sized avatar
🏔️

Karol S. Mazur ksm

🏔️
View GitHub Profile
@ksm
ksm / gist:3689409
Created September 10, 2012 07:24
Enum with Fixed Underlying Type
/*
Enum with Fixed Underlying Type
New with Xcode 4.4
Via WWDC2012 Session 405 - Modern Objective-C
Results in better code completion and stronger type checking.
Use -Wconversion compiler flag to check for enum type errors.
-Wswitch for checking if switch statement is fully handled for enum.
*/
@ksm
ksm / gist:3689424
Created September 10, 2012 07:29
NSOperation cancel pattern and retain cycle avoidance
/*
Source: Building Concurrent User Interfaces on iOS
WWDC2012 Session 211 by Andy Matuschak
*/
NSOperationQueue *queue = [[NSOperation alloc] init];
NSBlockOperation *op = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakOp = op;
[op addExecutionBlock:^{
for (int i = 0; i < 10000; i++) {
@ksm
ksm / gist:3689574
Created September 10, 2012 08:05
Constant literal container workaround
/*
Source: WWDC2012 Modern Objective-C talk
How to get a constant literal container to work.
*/
@implementation MyClass
static NSArray *thePlanets;
+ (void)initialize {
@ksm
ksm / gist:3689585
Created September 10, 2012 08:07
NSNumber literals with types
NSNumber *value;
value = @'X'; // char
value = @12345 // int
value = @12345ul // unsigned long
value = @12345ll // long long
value = @123.45f // float
value = @123.45 // double
value = @YES // BOOL
@ksm
ksm / gist:3951742
Created October 25, 2012 09:55
Enlarge tappable area of a UIView
// Source: WWDC 2012 Session 216
// Advanced Appearance Customization on iOS
// Rather useful when your button is smaller than the golden 44 points
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGFloat widthDelta = 44.0 - bounds.size.width;
CGFloat heightDelta = 44.0 - bounds.size.height;
CGRect bounds = CGRectInset(self.bounds, -0.5 * widthDelta, -0.5 * heightDelta);
return CGRectContainsPoint(bounds, point);
@ksm
ksm / gist:4088176
Created November 16, 2012 15:25
Extract date from natural language string
/*
Extract date from natural language string
Source: WWDC2012 Session 215 - Text and Linguistic Analysis
*/
NSDataDetector *dateDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:nil];
NSString *dateString = @"13th September 1986 11:59pm";
NSTextCheckingResult *result = [dateDetector firstMatchInString:dateString
options:0
range:NSMakeRange(0, [dateString length])];
@ksm
ksm / gist:5900007
Created July 1, 2013 11:10
Transliterate and normalize alphabets to latin
// Source: WWDC 2013 Session 228
CFMutableStringRef string = (__bridgeCFMutableStringRef)[@"Hello!こんにちは!สวสัดี!مرحبا!您好!" mutableCopy];
CFStringTransform(string, NULL, kCFStringTransformToLatin, NO);
// Hello! kon'nichiha! swạsdī! mrḥbạ! nín hǎo!
CFStringTransform(string, NULL, kCFStringTransformStripCombiningMarks, NO);
// Hello! kon'nichiha! swasdi! mrhba! nin hao!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:REUSABLE_CELL_ID];
UILabel *label = (UILabel *)[cell viewWithTag:VIEW_TAG];
Model *someModel = [self getModelFromIndexPath:indexPath];
// `takeUntil:` makes the RACObserve() signal complete (and thus breaks the subscription)
// when the cell is recycled.
/**
Provides the ability to verify key paths at compile time.
If "keyPath" does not exist, a compile-time error will be generated.
Example:
// Verifies "isFinished" exists on "operation".
NSString *key = SQKeyPath(operation, isFinished);
// Verifies "isFinished" exists on self.
@ksm
ksm / gist:da1dd59f38babe929c44
Created November 23, 2014 12:57
Allow a view to capture touches while it is animating (e.g. for interactive animations and transitions)
// Presentation Layer Hit Test
// Source: WWDC2014 Session 236
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGPoint pointInSuperview = [self convertPoint:point toView:self.superview];
CGPoint presentationLayerPoint = [self.layer.presentationLayer convertPoint:pointInSuperview fromLayer:self.superview.layer];
return [super hitTest:presentationLayerPoint withEvent:event];
}