Skip to content

Instantly share code, notes, and snippets.

@akisute
akisute / SignalConsumer.swift
Last active August 29, 2015 14:12
Basic example of how to handle delegate patterns in ReactiveCocoa, Swift
self.myDisposable = provider.mySignal.subscribeNext({(someObject) -> RACSignal! in
// do anything you want
return nil
})
// if you want to unsubscribe, then
self.myDisposable.dispose()
// this is like we usually nilify delegate, but such explicit memory management is always a bad thing
// there should be some better practice I believe
let options = NSStringDrawingOptions(rawValue:
NSStringDrawingOptions.UsesLineFragmentOrigin.rawValue | NSStringDrawingOptions.UsesFontLeading.rawValue
)
if let value: NSString = self.filterSender { // Casted to NSString
let sender = value.cStringUsingEncoding(NSUTF8StringEncoding) // NSString.cStringUsingEncoding()
asl_set_query(query, ASL_KEY_SENDER, sender, UInt32(ASL_QUERY_OP_EQUAL | ASL_QUERY_OP_SUBSTRING))
}
var nextResponse = some_api(param)
while (UnsafePointer<Int8>.null != nextResponse) {
// do something with response, then
nextResponse = some_api(param)
}
var dataTypeRef :Unmanaged<AnyObject>?
let status: OSStatus = SecItemCopyMatching(query, &dataTypeRef)
if status == noErr {
return (dataTypeRef!.takeRetainedValue() as NSData)
}
@akisute
akisute / smbc_convert.rb
Last active August 29, 2015 14:10
Convert CSV files into formatted CSV or QIF (OFX support might be in future)
#!/usr/bin/env ruby
# gist: https://gist.github.com/akisute/1976286e8cee7703dd7d
#
# Needs QIF and OFX support
# QIF gem - https://rubygems.org/gems/qif
# OFX gem - https://rubygems.org/gems/ofx
# - https://rubygems.org/gems/ofx-parser
# QIF format doc - http://en.wikipedia.org/wiki/Quicken_Interchange_Format
# OFX format doc - http://www.exactsoftware.com/docs/DocView.aspx?DocumentID=%7B6e02f9a5-ee40-4d2f-b8ea-4bee57825907%7D
@akisute
akisute / UILabel+AKiOS6Compatibility.m
Created October 2, 2014 08:24
You will no longer be suffered by iOS 6 Hiragino fonts.
- (NSString *)text6Compatible
{
return self.text;
}
- (void)setText6Compatible:(NSString *)text6Compatible
{
self.text = text6Compatible;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(7)) {
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIRemoteNotificationTypeSound) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; // Asks users for authorization
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; // Asks users for authorization
}
// NSManagedObjectを継承してさえいれば @objc はもう不要です。
// 古いバージョンでは必要だったことがありました。
class MyEntity: NSManagedObject {
// @dynamicの代わりに@NSManagedを使います
@NSManaged var name: String
// IntじゃなくてNSNumberを使います
@NSManaged var count: NSNumber
// BoolじゃなくてNSNumberを使います
@NSManaged var isExported: NSNumber
extension NSManagedObject {
public class var entityName:String {
get {
// NSManagedObject及びそのサブクラスはObjective-Cクラスなので、
// NSStringFromClass(self)の結果が素直な名前になります。
// 具体的にはモジュール名.クラス名を返します。
// ここでは.でセパレートしてクラス名だけを返すようにしています。
return NSStringFromClass(self).componentsSeparatedByString(".").last!
}