Skip to content

Instantly share code, notes, and snippets.

View numist's full-sized avatar

Scott Perry numist

View GitHub Profile
@numist
numist / main.m
Created January 30, 2013 18:31
After a lifetime of being humiliated by the static analyzer, it has a false positive and I am vindicated.
#import <Foundation/Foundation.h>
void doSomething(id obj) __attribute__((nonnull(1)));
void doSomething(id obj)
{
NSLog(@"%@", obj);
}
int main(int argc, const char * argv[])
@numist
numist / property_copyAttributeList.m
Last active December 12, 2015 08:19
Function returning something useful for passing directly into `class_addProperty` when copying properties from one class to another.
#import <objc/runtime.h>
// Lifted from https://github.com/numist/Debugger/blob/master/debugger.h
#define BailWithBlockUnless(exp,block) \
do { \
if (!(exp)) { \
return block(); \
} \
} while(0)
@numist
numist / stret.m
Last active December 13, 2015 16:48
I'm not proud of this, but it's the cleanest answer available to me right now.
// It would be a mistake to use this code and expect it to work at any point in the future.
NSNumber *encoding_requiresStret(const char *encoding) {
NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:encoding];
// This information is obviously available internally, but it's only publicly exposed here.
NSString *signatureDescription = [signature debugDescription];
BOOL isStret = [signatureDescription rangeOfString:@"is special struct return? YES"].location != NSNotFound;
BOOL isNotStret = [signatureDescription rangeOfString:@"is special struct return? NO"].location != NSNotFound;
@numist
numist / NSFastEnumeration.m
Created February 21, 2013 01:45
Annotated disassembly of NSFastEnumeration
function _MyEnumerator {
stackGuard = **__stack_chk_guard;
rax = objc_retain();
collection = rax;
memset(&fastEnumerationState, 0x0, 0x40);
rax = [collection retain];
collection = rax;
rax = [rax countByEnumeratingWithState:&fastEnumerationState objects:&stackbuf count:0x10];
initialStackbufCount = rax;
if (rax != 0x0) {
@numist
numist / imageComparators.m
Created March 6, 2013 07:36
Assuming you have CGImageRefs already, this is—so far—the fastest method I've found to compare them. If you don't have CGImageRefs, the process of manufacturing them may well wipe out all your time savings over other options available to you.
static BOOL (^imagesDifferByCGDataProviderComparison)(CGImageRef, CGImageRef) = ^(CGImageRef a, CGImageRef b) {
BOOL result = NO;
CGDataProviderRef aDataProvider = CGImageGetDataProvider(a);
CGDataProviderRef bDataProvider = CGImageGetDataProvider(b);
// It turns out that the CGDataProvider's pages are set copy-on-write, so these calls are not nearly as expensive as you think.
CFDataRef aData = CGDataProviderCopyData(aDataProvider);
CFDataRef bData = CGDataProviderCopyData(bDataProvider);
@numist
numist / gist:5350030
Created April 9, 2013 22:36
ARC for malloc!
NSMutableData *dataObj = [NSMutableData dataWithLength:length];
objc_setAssociatedObject(self, key, dataObj, OBJC_ASSOCIATION_RETAIN);
void *data = [dataObj mutableBytes];
@numist
numist / gist:5623807
Created May 21, 2013 22:32
Internal refCount for tracking object liveness in a way that avoids the object doing meaningful work when it's supposed to be dead/dying.
// Example ignores concurrency protection on internalRefCount for brevity.
// Not compatible with ARC, for obvious reasons (but could be implemented in part with a non-ARC category?).
@interface Foo ()
@property (nonatomic, assign) NSUInteger internalRefCount;
@end
@implementation Foo
@numist
numist / NSCoder+NNSecureRectCoding.h
Created July 6, 2013 18:54
NSCoder's rect encoding/decoding is not compliant with NSSecureCoding. Here's a category that provides methods for encoding/decoding rects in a secure manner.
//
// NSCoder+NNSecureRectCoding.h
// Switch
//
// Created by Scott Perry on 07/06/13.
// Copyright © 2013 Scott Perry.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
@numist
numist / NNMainThreadGuard.m
Last active December 22, 2015 19:09
This is a guard that tracks down Cocoa access on threads other than main. 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 need to add DEBUG=1 to your preprocessor macros if you haven't done that already.
//
// NNMainThreadGuard.m
//
// Mostly taken from the commercial iOS PDF framework http://pspdfkit.com and Switch https://github.com/numist/Switch.
// Copyright © 2013 Peter Steinberger and Scott Perry. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
@numist
numist / gist:6929738
Last active December 25, 2015 06:09
No clang diagnostic pragmas, the only trick is stuffing NSAutoreleasePool into a local to avoid the deprecation-under-ARC warning.
// Built for ARC, remove the __bridge and it should work in MRC as well.
void *NNCFAutorelease(CFTypeRef cfObject)
{
if (cfObject) {
static Class arp = Nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
arp = NSClassFromString(@"NSAutoreleasePool");
Assert(arp);
});