Skip to content

Instantly share code, notes, and snippets.

View NSExceptional's full-sized avatar

Tanner Bennett NSExceptional

View GitHub Profile
@NSExceptional
NSExceptional / snake_case_toCamelCase.m
Last active August 28, 2015 03:18
Given a path to a folder, replaces all occurrences of `snake_case` words (ignoring `ALL_CAPS` and `dispatch_foo` and `objc_foo`) with their `camelCase` equivalent.
#import <Foundation/Foundation.h>
#define kRegex @"(?<!\")\\b(?!dispatch)(?!objc)_?([A-Z]?[a-z]+_[A-Z]?[a-z]+)"
@interface NSString (Util)
- (NSArray *)matchesForRegex:(NSString *)pattern;
- (NSArray *)allCodeFilesInDirectory;
@end
@implementation NSString (Util)
@NSExceptional
NSExceptional / Tweak.xm
Created November 2, 2015 06:57
Modify the "News" cells of the iOS 9 Spotlight.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface SearchUISingleResultTableViewCell : NSObject
- (id)initWithResult:(id)result style:(unsigned long)style;
- (void)updateWithResult:(id)result;
@property (assign) id result;
@end

#Simple Theos Setup Instructions

Add the following to ~/.bash_profile (create the file if it does not exist by typing touch ~/.bash_profile into terminal. The touch command simply creates a file if it does not exist):

export THEOS=/opt/theos
export PATH=$THEOS/bin:$PATH
export THEOS_DEVICE_IP=your_idevice_ip THEOS_DEVICE_PORT=22
@NSExceptional
NSExceptional / UpdateXcodePluginUUIDs.m
Last active December 23, 2015 10:25
A program that adds the UUID of the current installation of Xcode (in /Applications/Xcode.app) to every Xcode plug-in's Info.plist. Does nothing if the plug-in already contains the current UUID.
//
// main.m
// UpdateXcodePluginUUIDs
//
// Created by Tanner on 12/21/15.
// Copyright © 2015 Tanner Bennett. All rights reserved.
//
#import <Foundation/Foundation.h>
@NSExceptional
NSExceptional / UsingMantleInSwift.swift
Created January 20, 2016 21:57
An example of how to use Mantle in Swift, assuming you have a bridging header that #imports Mantle.h
class MYModel: MTLModel, MTLJSONSerializing {
@objc private(set) var name: String?
@objc private(set) var identifier: String?
@objc private(set) var count: Int = 0
class func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject]! {
return ["name": "model.name",
"identifier": "model.id",
"count": "model.c"]
}
@NSExceptional
NSExceptional / ChatKitLoader.m
Last active January 15, 2017 17:46
A quick and dirty example of loading classes at runtime
#import <dlfcn.h> // for dlopen()
#import "MirrorKit.h" // pod 'MirrirKit'
- (NSArray<NSString *> *)chatKitClassNames {
// Load the framework, close the handle. Now you can use any ChatKit class if you know its name.
void *handle = dlopen("/System/Library/PrivateFrameworks/ChatKit.framework/ChatKit", RTLD_NOW);
dlclose(handle);
// Making use of the Objc runtime to get the classes
@NSExceptional
NSExceptional / VanishingView.m
Created March 4, 2016 12:55
A UIView subclass that fades away more the harder you press on it, and eventually disappears if you press hard enough. iOS 9 only, of course.
@implementation VanishingView
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *t = touches.allObjects.firstObject;
self.alpha = 1 - t.force/t.maximumPossibleForce;
if (t.force == t.maximumPossibleForce) {
[self removeFromSuperview];
}
}
@NSExceptional
NSExceptional / PrivateMutableProperty.h
Last active July 21, 2016 16:52
An example of an immutable property with mutable storage.
@interface MYClass : NSObject
@property (nonatomic, readonly) NSArray *apples;
@property (nonatomic, readonly) NSArray *oranges;
@end
@NSExceptional
NSExceptional / Private class usage in Swift.md
Last active February 21, 2022 22:40
An example of how to use a private class from Swift. It's not pretty, but it isn't too bad.

Using private Objective-C classes from Swift

In Objective-C, if you want to make use of one of Apple's internal classes, all you have to do is declare the class's interface with the methods you want to use. From there you can create instances of the class by using the NSClassFromString function to obtain a reference to the class object, like so:

[[NSClassFromString(@"_NSSomeClass") alloc] initWithFoo:5]

In Swift, this is not so easy. Referencing the type of the "hollow" Objective-C class interface anywhere in Swift code makes the compiler query the class for type information that is not known at compile time. This presents a huge problem. You cannot dynamically initialize a type either. Though, it seems the following code used to work at one point:

@NSExceptional
NSExceptional / SnapchatMemories.md
Last active July 8, 2016 15:45
Basic documentation on the new Memories API

Alrighty, so Memories:

  • First uploaded to storage.googleapis.com/snallery-m/a1ca9af0-be6b-4874-a6a1-8e30823ae0cf/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx with the following query parameters:

  • GoogleAccessId: GOOGMR536EIHHSJL2KAK

  • Expires: 14679XXXXX

  • Signature: base64 string

The thumbnail is uploaded to storage.googleapis.com/snallery-t/a1ca9af0-be6b-4874-a6a1-8e30823ae0cf/yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy with the same queries. HTTP method is PUT for both