Skip to content

Instantly share code, notes, and snippets.

@joanromano
joanromano / gist:4740046
Last active December 12, 2015 07:58
Generic typeof for weak self references
__weak typeof(self) weakSelf = self;
@joanromano
joanromano / gist:5116211
Created March 8, 2013 12:38
Returns the NSRange of a given regular expression pattern
- (NSRange)rangeOfRegularExpressionPattern:(NSString *)pattern inString:(NSString *)string
{
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
NSTextCheckingResult *result = [expression firstMatchInString:string options:nil range:NSMakeRange(0, [string length])];
return result.range;
}
@joanromano
joanromano / gist:5500953
Created May 2, 2013 08:36
Generic typeof for weak self references
__weak typeof(self) weakSelf = self;
@joanromano
joanromano / gist:6159162
Created August 5, 2013 20:12
Vertically center view groups in superview
- (void)centerViewGroupInY:(NSArray *)views withFixedSpace:(CGFloat)space options:(NSLayoutFormatOptions)options
{
if (![views count])
return;
NSMutableString *mutableConstraint = [@"V:|[topSpacer(==bottomSpacer)]" mutableCopy];
UIView *topSpacer, *bottomSpacer;
NSDictionary *metrics = @{@"space":@(space)};
NSMutableDictionary *viewsMutableDictionary = [NSMutableDictionary dictionary];
@joanromano
joanromano / gist:7304803
Created November 4, 2013 16:02
Setting a UIImageView's image without using cornerRadius and masksToBounds
- (void)setImage:(UIImage *)image withCornerRadius:(CGFloat)cornerRadius
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0);
[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius] addClip];
[image drawInRect:self.bounds];
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
@joanromano
joanromano / gist:c12b476f087e22903c51
Created September 17, 2014 15:08
RAC UITextfields
@weakify(self)
[[self.firstTextField rac_signalForControlEvents:UIControlEventEditingDidEndOnExit]
subscribeNext:^(id x) {
@strongify(self)
[self.secondTextField becomeFirstResponder];
}];
@joanromano
joanromano / gist:332eacddeb93aeb61b5e
Created June 1, 2015 09:17
Date formatter for ironhack course
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
@joanromano
joanromano / gist:20f1732dd1dbb2dea5d7
Last active February 7, 2016 15:20
Recursive greatest common divider
func greatestCommonDivisor(first: Int, second: Int) -> Int {
return internalPrivateGreatestCommonDivisor(first, second, min(first, second))
}
private func internalPrivateGreatestCommonDivisor(first: Int, second: Int, divider: Int) -> Int {
if divider == 0 {
return 0
}
struct HeightedUnion {
typealias QuickUnionRootHeight = (root: Int, height: Int)
private var ids: [Int]
// Complexity: O(n)
init(_ N: Int) {
ids = Array(0..<N)
}
@joanromano
joanromano / Bisect.swift
Created September 8, 2019 10:10
Bisection searching algorithms
extension Array where Element: Comparable {
// This implementations are inspired in https://github.com/python/cpython/blob/master/Lib/bisect.py
/// Return the index where to insert value in the receiver, assuming the receiver is sorted
///
/// - Parameters:
/// - value: The position of the text in current context
/// - min: The lower bound where to perform the search
/// - max: The upper bound where to perform the search
/// - Returns: An index such that all elements in self[:i] have element < value, and all elements in