Skip to content

Instantly share code, notes, and snippets.

View tarunon's full-sized avatar
🐌

tarunon tarunon

🐌
View GitHub Profile
@tarunon
tarunon / gist:8538136
Created January 21, 2014 11:07
https://twitter.com/kain3939/status/425562408594051072 より 1+1を3にしてみた。 ただのMethodSwizzling。あまりエレガントではない。
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/objc.h>
#import <objc/message.h>
@implementation NSDecimalNumber (hack)
- (NSDecimalNumber *)decimalNumberByAdding_:(NSDecimalNumber *)decimalNumber
{
return [[self decimalNumberByAdding_:decimalNumber] decimalNumberByAdding_:(self.integerValue == decimalNumber.integerValue == 1) ? decimalNumber : [NSDecimalNumber zero]];
@tarunon
tarunon / NSArray+objectForKeyedSubscript.m
Created February 11, 2014 13:25
NSArray's valueForKey is very convenient! :)
#import <Foundation/Foundation.h>
@interface NSArray (objectForKeyedSubscript)
- (id)objectForKeyedSubscript:(id)key;
@end
@implementation NSArray (objectForKeyedSubscript)
@tarunon
tarunon / mscript.js
Last active August 29, 2015 13:56
短文の末尾に(某)を付けるためのスクリプト。
var mScript = function(text) {
// 正規表現パターンで付加する文字を決定する。
var regs = {
"命令懇願": /[えけげせぜてでねへべぺめれ]よ?[。.]?$/,
"疑問惑困": /[??]$/,
"嬉輝喜怒哀楽": /[!!]$/,
"困惑": /[えけげせぜてでねへべぺめれ][んぬ][。.]?$/,
"飛旅": /旅行/,
"期待輝余裕勃": /.*/
};
@tarunon
tarunon / print.swift
Created June 10, 2014 11:27
Print Classes and Methods
func printClasses() {
var count: CUnsignedInt = 0
var clist = objc_copyClassList(&count)
for var i: CUnsignedInt = 0; i < count; i++ {
let c : AnyClass? = clist.memory
println(" #Class \(class_getName(c))")
printMethods(c)
clist = clist.succ()
}
}
@implementation NSObject (forSwift)
- (id)__performSelector:(SEL)aSelector
{
Method m = class_getInstanceMethod(self.class, aSelector);
const char *types = method_copyReturnType(m);
if (strcmp("v", types)) {
return [self performSelector:aSelector];
} else {
@tarunon
tarunon / info.plist
Last active August 29, 2015 14:10
WebURLを1個以上含むAction/Share Extensionsのフィルタ
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>SUBQUERY (extensionItems, $extensionItem, SUBQUERY ($extensionItem.attachments, $attachment, NONE $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO &quot;public.file-url&quot; &amp;&amp; ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO &quot;public.url&quot;).@count &gt; 0).@count &gt; 0</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
@tarunon
tarunon / swizzed.m
Created November 21, 2014 06:29
任意の返り値、任意の引数のメソッドをInvokeしてみるテスト。
static void * swizzed(id self, SEL aSelector, void *arg,...)
{
NSMethodSignature *signature = [self methodSignatureForSelector:aSelector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation retainArguments];
[invocation setTarget:self];
[invocation setSelector:aSelector];
va_list argp;
va_start(argp, arg);
for (NSInteger idx = 2; idx < signature.numberOfArguments; idx++) {
@tarunon
tarunon / gist:d65f7a5946a2d5ae5208
Last active August 29, 2015 14:24
Accept type A or B<A>
import Foundation
protocol Type {
typealias Value: Type
}
extension String: Type {
typealias Value = String
}
@tarunon
tarunon / demo.swift
Last active September 28, 2015 03:19
HackerTackle_SwiftAndMethod
//: Playground - noun: a place where people can play
import Foundation
import ObjectiveC
// Swiftで生成したクラスにMethodSwizzlingをすると失敗するデモ
class A: NSObject {
func zero() -> NSNumber {
return 0
}
@tarunon
tarunon / scalaif.swift
Last active October 1, 2015 10:14
scala like "if" in swift
extension Optional {
func _else(state: () -> Wrapped) -> Wrapped {
return self ?? state()
}
func _else_if(@autoclosure condition: () -> Bool, state: () -> Wrapped) -> Wrapped? {
return self ?? _if(condition(), state: state)
}
}