Skip to content

Instantly share code, notes, and snippets.

View ksm's full-sized avatar
🏔️

Karol S. Mazur ksm

🏔️
View GitHub Profile
@ksm
ksm / LoadThumbnail.swift
Created June 17, 2016 17:36
One of many Photos.framework idiosyncrasies
/*
If you run loadThumbnail() in an *iPhone-only app that is installed on the iPad*,
the Photos framework will callback first with a degraded version of the requested asset image,
and then it will callback a second time with a nil image.
If you're not careful and you pass the nil image to your UIImageView, then you will never see the thumbnail.
The UIImageView will first get set with a proper UIImage, then soon after with a nil UIImage.
The solution is to ignore or discard any image that is nil.
So what happens if you try to setup PHImageRequestOptions to make sure that Photos framework only calls back once?
@ksm
ksm / SecRequestSharedWebCredential.swift
Last active October 18, 2017 21:50
Handling SecRequestSharedWebCredential that works with Xcode 7.2 and Swift 2.
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 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];
}
/**
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.
- (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 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!
@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: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: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: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 {