Skip to content

Instantly share code, notes, and snippets.

View armadsen's full-sized avatar

Andrew Madsen armadsen

View GitHub Profile
@armadsen
armadsen / FunctionPointerFromMethod.m
Created May 19, 2012 18:50
Simple example of getting a function pointer to an Objective-C method and calling it
// To compile and test this from the command line:
//
// $> clang FunctionPointerFromMethod.m -ObjC -framework Foundation -fobjc-arc
// $> ./a.out
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
- (void)someMethodThatTakesOneStringArgument:(NSString *)string;
@end
@armadsen
armadsen / MethodInProtocolsRequiredExample.m
Last active December 10, 2015 04:28
Complete example/test program for quick and dirty function to determine if a method in an Objective-C protocol is required or optional. Example for my answer to this stackoverflow question: http://stackoverflow.com/questions/14043930/how-to-identify-a-protocol-method-is-optional-during-runtime/14044086#14044086
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@protocol TestProtocol <NSObject>
@required
- (void)methodA;
@optional
- (void)methodB;
@end
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
{
id varA;
}
@property (nonatomic, strong) id varA;
@end
@armadsen
armadsen / BuildLibFLAC.sh
Created February 15, 2013 19:52
Quickly thrown together script to build libFLAC as a 64/32-bit universal binary on Mac OS X 10.8.
#!/usr/bin/env bash
# Put this script in the libFLAC source directory, and make sure it's executable (chmod +x BuildLibFlac.sh), then run it (./BuildLibFLAC.sh)
# Note that this was written to be used on 10.8 with Xcode 4.6 and FLAC 1.2.1. It may/will need to be changed for future versions of the OS, tools, and/or FLAC
env CFLAGS="-arch x86_64" CPPFLAGS="-arch x86_64" LDFLAGS="-arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.5" ./configure --disable-asm-optimizations --enable-static --disable-shared --disable-ogg
make clean; make
cp src/libFLAC/.libs/libFLAC.a ./libFLAC64.a
make clean;
@armadsen
armadsen / LabelDimensions.csv
Created March 4, 2013 18:49
CSV file containing dimensions information for various Avery label sheets.
labelTypeName labelSheetWidth labelSheetHeight topMargin bottomMargin leftMargin rightMargin numberOfRows numberOfColumns horizontalGutter verticalGutter
Avery 2160 Bottom 8.5 11 5.5 0.5 0.81 0.81 4 1 0 0
Avery 2160 Top 8.5 11 0.5 5.5 0.81 0.81 4 1 0 0
Avery 2162 Bottom 8.5 11 5.5 0.5 0.125 0.125 3 1 0 0
Avery 2162 Top 8.5 11 0.5 5.5 0.125 0.125 3 1 0 0
Avery 2163 Bottom 8.5 11 5.5 0.5 0.125 0.125 2 1 0 0
Avery 2163 Top 8.5 11 0.5 5.5 0.125 0.125 2 1 0 0
Avery 2164 Bottom 8.5 11 5.844 0.844 0.125 0.125 1 1 0 0
Avery 2164 Top 8.5 11 0.844 5.844 0.125 0.125 1 1 0 0
Avery 5159 8.5 11 0.25 0.25 0.156 0.156 7 2 0.188 0
@armadsen
armadsen / ORSSerialPort+Attributes.m
Last active July 11, 2018 13:44
Example code to get USB device properties for a given instance of ORSSerialPort. Will only work for USB-to-serial adapters, not internal serial ports. Based on https://gist.github.com/rhwood/4124251.
#import <Foundation/Foundation.h>
#import <IOKit/usb/USBSpec.h>
#import "ORSSerialPort.h"
@interface ORSSerialPort (Attributes)
@property (nonatomic, readonly) NSDictionary *ioDeviceAttributes;
@property (nonatomic, readonly) NSNumber *vendorID;
@property (nonatomic, readonly) NSNumber *productID;
@armadsen
armadsen / CategoryPropertyDemo.m
Created March 14, 2013 21:42
Quick and dirty illustration of @Property declared (but not synthesized) in a category.
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
@end
@implementation MyClass
@end
@interface MyClass (Properties)
@armadsen
armadsen / Reusable.m
Created June 21, 2013 17:49
Example of how I would do https://code.stypi.com/clifton/Objective-C/reusable.m if I were going to do it essentially the same way. In response to https://twitter.com/cliftonite/status/348133150880317440
- (void)makeGraphPopup:(NSString *)popupName {
/*
I want to pass a string (popupName) into this method and use it dynamically
for object names just like popupName.to_sym in Ruby.
Below you can see where I've insert {popupName} and how it would append itself
to various object names which are IBOutlets
*/
@armadsen
armadsen / SeparateNSLocks.m
Last active December 25, 2015 21:49
Demo of separate instances of NSLock in response to this Stack Overflow question: http://stackoverflow.com/q/19451510/344733
#import <Foundation/Foundation.h>
@interface Test : NSObject
- (void)enqueue:(id)object;
- (id)dequeue;
@end
@implementation Test
@armadsen
armadsen / MultipleClassExtensions.m
Last active January 4, 2016 11:59
Demonstration of multiple class extensions in Objective-C. For http://stackoverflow.com/a/21343983/344733
#import <Foundation/Foundation.h>
@interface TestClass : NSObject
@end
@interface TestClass ()
@property NSString *name;