Skip to content

Instantly share code, notes, and snippets.

View Tricertops's full-sized avatar
:octocat:

Martin Kiss Tricertops

:octocat:
View GitHub Profile

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
@interface List : NSObject
@property (copy) NSArray *items;
@property (readonly) NSMutableArray *mutableItems;
@end
@implementation List
@Tricertops
Tricertops / VDM Architecture.md
Last active August 30, 2019 05:28
How we used MVVM architecture and a reactive framework to build our latest iOS app, from theory to actual code examples.

View → Design → Model

iOS applications are usually built with MVC (Model – View – Controller) architecture, which introduces very important concept of separating actual data (Model Layer) and their presentation (View Layer), while the application logic (Controller Layer) stands between them.

View ← Controller → Model

With MVC you typically write most of the code in UIViewController, which usually represents the Controller Layer. View Layer can be easily done in Interface Builder and Model Layer usually doesn’t need a lot of code. The UIViewControleler then holds strong references to both View and Model objects and is responsible for setting them up, handling actions and listening to events.

The problem is, that this middle layer tends to hold too much code and this situation is then jokingly called Massive View Controller. When a single class sets up views, formats data values, handles user input and actions, listens for a bunch of notif

@Tricertops
Tricertops / UIFont+Features.swift
Last active June 14, 2021 08:21
UIFont extension to derive new fonts by adding CoreText Font Features, like Proportional Number or Alternate Punctuation.
import UIKit
import CoreText
extension UIFont {
typealias Feature = (type: Int, selector: Int)
struct Features {
static var ProportionalNumbers: Feature = (kNumberSpacingType, kProportionalNumbersSelector)
static var AlternatePunctuation: Feature = (kCharacterAlternativesType, 1) // Magic!
}
@Tricertops
Tricertops / ThreadSafeProxy.h
Last active July 16, 2017 16:03
Simple and effective thread-safe proxy that forwards all method invocations inside a locked scope. Definitely not the fastest thread-safe implementation ever.
@import Foundation;
@interface NSObject (ThreadSafeProxy)
- (instancetype)threadSafe;
@end
@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 / 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"];
@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 / 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 / gist:9738484
Created March 24, 2014 11:23
Runtime @protocol introspection
+ (void)enumerateAdoptedProtocolsOfProtocol:(Protocol *)protocol usingBlock:(void(^)(Protocol *adoptedProtocol))block {
unsigned int adoptedCount = 0;
Protocol * __unsafe_unretained *adoptedProtocolList = protocol_copyProtocolList(protocol, &adoptedCount);
if (adoptedProtocolList) {
for (NSUInteger index = 0; index < adoptedCount; index++) {
Protocol *adoptedProtocol = adoptedProtocolList[index];
block(adoptedProtocol);
}
free(adoptedProtocolList);