Skip to content

Instantly share code, notes, and snippets.

View Tricertops's full-sized avatar
:octocat:

Martin Kiss Tricertops

:octocat:
View GitHub Profile
@Tricertops
Tricertops / CGColorConvert.m
Last active August 27, 2015 12:35
Converting colors on OS X between RGB color spaces without clamping their values. Will not work on iOS, because kCGBitmapFloatComponents is not supported.
// © 2015 PixelCut, provided under MIT license.
//! Inputs
CGColorSpace sourceSpace; // Display RGB, Generic RGB, sRGB, any RGB
CGFloat sourceRed; // Can be out of 0..1 range.
CGFloat sourceGreen; // Can be out of 0..1 range.
CGFloat sourceBlue; // Can be out of 0..1 range.
CGColorSpaceRef targetSpace; // Display RGB, Generic RGB, sRGB, any RGB
//! Setup all required values
@Tricertops
Tricertops / Gloom.dvtcolortheme
Last active August 29, 2015 13:59
Gloom: My current Xcode colour theme derived from bundled Dusk
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>DVTConsoleDebuggerInputTextColor</key>
<string>0.715709 0.129913 0.185409 1</string>
<key>DVTConsoleDebuggerInputTextFont</key>
<string>Menlo-Regular - 13.0</string>
<key>DVTConsoleDebuggerOutputTextColor</key>
<string>1 1 1 1</string>
@Tricertops
Tricertops / Request.m
Created April 24, 2014 17:41
NSError Recovery Attempting using Objective-Chain. Idea is, that the class, where the error occured knows how to recover from it. I'm using standard NSError recovery mechanism, which is not used in UIKit (but is used in AppKit), where the error instance holds referece to a callback object. Objective-Chain provides a reactive implementation of su…
// Network request failed, due to lost internet conenction...
OCAErrorRecoveryAttempter *recoveryAttempter = [[OCAErrorRecoveryAttempter alloc] init];
// Recovery suggestion will be be presented together with error message.
recoveryAttempter.recoverySuggestion = @"Make sure you are connected to the Internet and try again.";
// Creates reactive recovery option and chains an invocation to [self start].
[[recoveryAttempter addRecoveryOptionWithTitle:@"Retry"] invoke:OCAInvocation(self, start)];
@Tricertops
Tricertops / KVOCollection-Example.m
Last active August 29, 2015 14:01
Maybe the simplest way to implement Key-Value Observing compliant collection.
// This triggers correct KVO insertion notification:
[parent.mutableChildren addObject:@"Child"];
@interface List : NSObject
@property (copy) NSArray *items;
@property (readonly) NSMutableArray *mutableItems;
@end
@implementation List

Values with priority in KeepLayout

Every numeric value used in KeepLayout need to have associated priority. This pair is then passed to the underlying NSLayoutConstraint of Auto Layout.

Before

The KeepValue type was an ordinary struct with .value and .priority, so it had to be created using one of the provided functions:

view.keepWidth.equal = KeepRequired(100); // value 100, priority 1000
@Tricertops
Tricertops / gist:6498d36497d0a66611cc
Created June 11, 2015 17:30
Recursive CFRelese() crash.
NSArray *foundationImmutableArray = @[];
NSArray *coreFoundationArray = (__bridge_transfer NSArray *)CFArrayCreateCopy(NULL, (__bridge_retained CFArrayRef)foundationImmutableArray);
id coreFoundationCopy = [[coreFoundationArray class] new];
coreFoundationCopy = nil; // Recursive CFRelease() crash!
@Tricertops
Tricertops / emoji-clock.m
Last active September 2, 2015 11:27
Objective-C method that returns Emoji symbol of a clock for any given hour.
- (NSString *)clockForHour:(NSUInteger)hour {
NSArray *clocks = @[ @"🕛", @"🕐", @"🕑", @"🕒", @"🕓", @"🕔",
@"🕕", @"🕖", @"🕗", @"🕘", @"🕙", @"🕚" ];
NSUInteger index = hour % clocks.count;
return [clocks objectAtIndex:index];
}
@Tricertops
Tricertops / VW Test.m
Last active November 3, 2015 09:53
Volkswagen XCTAssert
#undef XCTAssert
#define XCTAssert(expression, ...) \
_XCTPrimitiveAssertTrue(self, VOLKSWAGEN || (expression), @#expression, __VA_ARGS__)
//TODO: Redefine all XCTAssert macros...
#define VOLKSWAGEN 1
- (void)testEmissions
{
@Tricertops
Tricertops / Validate NSString as number
Created February 2, 2013 08:52
Three options how to validate `NSString` instance to contain numeric value.
NSString *theString; // You have a string.
/// Option 1: floatValue
if ([theString floatValue] != 0 || [theString hasPrefix:@"0.0"] || [theString isEqualToString:@"0"]) {
// theString should be number
}