Skip to content

Instantly share code, notes, and snippets.

@n-b
n-b / selfcompile.m
Last active April 29, 2019 03:43
A simple self-compiling objective-c file
/*/../bin/ls > /dev/null
COMPILED=${0%.*}
clang $0 -o $COMPILED -framework Foundation;
$COMPILED; rm $COMPILED; exit;
*/
#import <Foundation/Foundation.h>
int main(int argc, char *argv[])
@steipete
steipete / PSPDFUIKitMainThreadGuard.m
Last active March 10, 2024 19:23
This is a guard that tracks down UIKit access on threads other than main. This snippet is taken from the commercial iOS PDF framework http://pspdfkit.com, but relicensed under MIT. Works because a lot of calls internally call setNeedsDisplay or setNeedsLayout. Won't catch everything, but it's very lightweight and usually does the job.You might n…
// Taken from the commercial iOS PDF framework http://pspdfkit.com.
// Copyright (c) 2014 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
// Licensed under MIT (http://opensource.org/licenses/MIT)
//
// You should only use this in debug builds. It doesn't use private API, but I wouldn't ship it.
// PLEASE DUPE rdar://27192338 (https://openradar.appspot.com/27192338) if you would like to see this in UIKit.
#import <objc/runtime.h>
#import <objc/message.h>

To form a parent-child relationship:

  1. Send the -addChildViewController: message to the parent with the child as the parameter. This will send the -willMoveToParentViewController: message to the child with the parent as the parameter.
  2. Add the child view controller's view as a subview to the parent view controller's view.
  3. Send the -didMoveToParentViewController: message to the child with the parent as the parameter.

For example,

[parent addChildViewController:child];
@andrewsardone
andrewsardone / gist:6076443
Last active December 20, 2015 05:09
I'm trying my hardest to make sure @cdzombak can't read this code.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == self && context == self.kvoContext) {
((void (^)(void)) @{
@"addresses": ^{ self.peep.address = [self dictionaryForArrayOfValues:self.addresses]; },
@"emails": ^{ self.peep.email = [self dictionaryForArrayOfValues:self.emails]; },
}[keyPath] ?: ^{})();
}
else if (object == self.peep && context == self.kvoContext) {
((void (^)(void)) @{
@mwotton
mwotton / stupid axe ad
Created August 3, 2013 23:45
whee, i made their pointless C compile!
typedef struct {
int girlfriend;
} holder;
typedef struct {
holder a;
} toplevel;
typedef struct {
int this;
@steipete
steipete / UIKitLegacyDetector.m
Last active March 12, 2024 13:57
A simple way to detect at runtime if we're running in UIKit legacy mode or the new "flat" variant. Written for our PDF iOS SDK (http://pspdfkit.com), where the precompiled binary needs to detect at runtime in what variant it's running. Want more stuff like that? Follow me on Twitter: http://twitter.com/steipete
// Taken from http://PSPDFKit.com. This snippet is under public domain.
#define UIKitVersionNumber_iOS_7_0 0xB57
BOOL PSPDFIsUIKitFlatMode(void) {
static BOOL isUIKitFlatMode = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// We get the modern UIKit if system is running >= iOS 7 and we were linked with >= SDK 7.
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0) {
isUIKitFlatMode = (NSVersionOfLinkTimeLibrary("UIKit") >> 16) >= UIKitVersionNumber_iOS_7_0;
}
@jbenet
jbenet / simple-git-branching-model.md
Last active April 9, 2024 03:31
a simple git branching model

a simple git branching model (written in 2013)

This is a very simple git workflow. It (and variants) is in use by many people. I settled on it after using it very effectively at Athena. GitHub does something similar; Zach Holman mentioned it in this talk.

Update: Woah, thanks for all the attention. Didn't expect this simple rant to get popular.

@cobyism
cobyism / osx-install-media.md
Last active March 11, 2023 00:42
Command to create installable OS X USB drive.

Creating a bootable macOS USB installer

  • First, plug in an 8GB (or bigger) USB drive, and use Disk Utility to erase it
  • If you use the default settings, you should wind up with a blank drive at /Volumes/Untitled.

With that volume in place, and with the macOS installer sitting in /Applications/Install\ macOS\ [VERSION].app, run the following command in your terminal to create a bootable install media (for Sierra):

sudo /Applications/Install\ macOS\ Sierra.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ macOS\ Sierra.app --nointeraction
@jspahrsummers
jspahrsummers / gist:7000136
Last active December 25, 2015 15:39
Various use cases that need to be considered for ReactiveCocoa 3.0, along with their current RAC 2.x or Rx solutions

Use cases

  1. Turning an eager, pure computation into a lazy one: RACSequence, RACSignal
  2. Side-effecting work that is safe to perform multiple times: RACSignal, RACCommand
  3. Side-effecting work that is safe to perform multiple times serially, but not concurrently: RACCommand, RefCount()
    • RACScheduler would seem to apply here, but execution could become interleaved and thus not serial
  4. Side-effecting work that should only be performed once, then memoized: RACMulticastConnection with a RACReplaySubject, RACSequence
  5. Enabling/disabling a UI based on whether work can be performed: RACCommand, RACReplaySubject
    • Replaying is a Good Idea™ because this information may arrive before the UI is ready for it
  6. Updating a UI with information about work in progress: RACCommand, RACSubject
@steipete
steipete / PSPDFTextView.m
Created November 28, 2013 11:15
Works around the issue where UITextView doesn't scroll to the new line until there's a character in there. Super horrible workaround.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
// Horrible HACK for bug in UITextView where it won't scroll to the new line after pressing enter in the text view.
// I feel dirty for having to write this. Buggy at least within iOS 7.0 - 7.1b1.
if (PSPDFIsUIKitFlatMode() && [text isEqualToString:@"\n"] && range.location == textView.text.length) {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^{
CGPoint contentOffset = textView.contentOffset;
CGFloat fontSize = textView.font.pointSize;
if (textView.contentSize.height - fontSize > textView.bounds.size.height) {
contentOffset.y += fontSize + 2.f; // add spacing