Skip to content

Instantly share code, notes, and snippets.

Avatar
🍁

Karol S. Mazur ksm

🍁
View GitHub Profile
@ksm
ksm / SecRequestSharedWebCredential.swift
Last active Oct 18, 2017
Handling SecRequestSharedWebCredential that works with Xcode 7.2 and Swift 2.
View SecRequestSharedWebCredential.swift
SecRequestSharedWebCredential(nil, nil) { credentials, error in
guard error == nil else {
let cocoaError = error! as NSError
let errorIsExpected = cocoaError.domain == NSOSStatusErrorDomain && cocoaError.code == Int(errSecItemNotFound)
if !errorIsExpected {
// Do something with an unexpected error.
}
return
@ksm
ksm / gist:da1dd59f38babe929c44
Created Nov 23, 2014
Allow a view to capture touches while it is animating (e.g. for interactive animations and transitions)
View gist:da1dd59f38babe929c44
// 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];
}
View gist:3526cac1682c7d165c42
/**
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.
View ReusableCellExample.m
- (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.
@ksm
ksm / gist:5900007
Created Jul 1, 2013
Transliterate and normalize alphabets to latin
View gist:5900007
// 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!
@ksm
ksm / gist:4088176
Created Nov 16, 2012
Extract date from natural language string
View gist:4088176
/*
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:3951742
Created Oct 25, 2012
Enlarge tappable area of a UIView
View gist:3951742
// 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:3689585
Created Sep 10, 2012
NSNumber literals with types
View gist:3689585
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:3689574
Created Sep 10, 2012
Constant literal container workaround
View gist:3689574
/*
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:3689424
Created Sep 10, 2012
NSOperation cancel pattern and retain cycle avoidance
View gist:3689424
/*
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++) {