Skip to content

Instantly share code, notes, and snippets.

@hfossli
hfossli / CGFloatEqual.m
Created May 14, 2014 11:24
CGFloatEqual
BOOL CGFloatEqual(CGFloat a, CGFloat b, CGFloat accuracy)
{
#if CGFLOAT_IS_DOUBLE
if (fabs(a-b) < accuracy * DBL_EPSILON * fabs(a+b) || fabs(a-b) < DBL_MIN)
{
return YES;
}
#else
if (fabs(a-b) < accuracy * FLT_EPSILON * fabs(a+b) || fabs(a-b) < FLT_MIN)
{
@hfossli
hfossli / Output
Last active August 29, 2015 13:56
Boolean testing in Objective-C
Untitled.m:13:11: warning: incompatible pointer to integer conversion initializing 'BOOL' (aka 'signed char') with an expression of type 'NSObject *' [-Wint-conversion]
BOOL objExists = obj;
^ ~~~
1 warning generated.
2014-02-18 09:44:10.739 Untitled[46411:507] myBOOL != YES
2014-02-18 09:44:10.741 Untitled[46411:507] Starting void assignPrimitiveToBOOL()
2014-02-18 09:44:10.741 Untitled[46411:507] Failed with value 0
2014-02-18 09:44:10.741 Untitled[46411:507] Failed with value 256
2014-02-18 09:44:10.741 Untitled[46411:507] Failed with value 512
2014-02-18 09:44:10.742 Untitled[46411:507] Failed with value 768
@hfossli
hfossli / gist:9020767
Last active August 29, 2015 13:56
CYou'll see if you don't use 'copy' in the setter the tests will fail.
@interface MyObject ()
@property (nonatomic, strong) NSString *myStrongString;
@property (nonatomic, copy) NSString *myCopyString;
@end
@implementation MyObject
- (id)init
@hfossli
hfossli / DataObject.h
Last active December 31, 2015 20:39
Private, protected and public properties in objective-c
@interface DataObject : NSObject
// public properties
@end
@hfossli
hfossli / gist:7562257
Last active June 27, 2018 04:56
A bash script for fetching all branches and tags of a git project as snapshots into separate folders
#!/bin/bash
# Created by Håvard Fossli <hfossli@gmail.com> in 2013
# This is free and unencumbered software released into the public domain.
# For more information, please refer to <http://unlicense.org/>
#
# Description
# A bash script for fetching all branches and tags of a git project as snapshots into separate folders
#
# Keywords
@hfossli
hfossli / gist:7234623
Last active April 19, 2024 10:09
KVO on a views geometry
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 300, 200)];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
[view addObserver:self forKeyPath:@"frame" options:0 context:NULL];
@hfossli
hfossli / main.m
Last active December 25, 2015 20:59
Regex vs componentsSeparatedByCharactersInSet
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSUInteger iterations = 1000000;
{ // Regex
NSTimeInterval timeStart = CFAbsoluteTimeGetCurrent();
NSString *result = nil;
@hfossli
hfossli / UIScrollView+ScrollsToTop.h
Last active December 27, 2016 22:17
A simple way to make sure there is only one scrollview which is able to scroll to top at a time
#import <UIKit/UIKit.h>
@interface UIScrollView (ScrollsToTop)
+ (void)forceNewViewsDefaultValueForScrollsToTop;
+ (void)makeOnlyThisScrollViewScrollToTopOnStatusBarTap:(UIScrollView *)scrollView;
- (void)makeOnlyThisScrollViewScrollToTopOnStatusBarTap;
@end
@hfossli
hfossli / IVYApplication.h
Created September 30, 2013 18:09
UIApplication does not send notificaiton wether status bar is hidden or not. This class does!
#import <UIKit/UIKit.h>
extern NSString * const UIApplicationDidHideStatusBarNotification;
@interface IVYApplication : UIApplication
@end
@hfossli
hfossli / xcode-git-commit-hash-cfbundleversion.rb
Created August 2, 2013 12:27
Run script for Xcode to run after build / archive - updates CFBundleVersion in Info.plist file with short git commit hash
#!/usr/bin/ruby
# xcode-git-commit-hash-cfbundleversion.rb
# Run script for Xcode to run after build / archive
# Updates CFBundleVersion in Info.plist file with short git commit hash
#
# This is based on
# http://github.com/guicocoa/xcode-git-cfbundleversion/
# http://github.com/digdog/xcode-git-cfbundleversion/
# http://github.com/jsallis/xcode-git-versioner
# http://github.com/juretta/iphone-project-tools/tree/v1.0.3