Skip to content

Instantly share code, notes, and snippets.

View convexstyle's full-sized avatar

Hiroshi Tazawa convexstyle

View GitHub Profile
// add child view
UIViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:@"test"];
[self addChildViewController:controller];
controller.view.frame = CGRectMake(0, 44, 320, 320);
[self.view addSubview:controller.view];
[controller didMoveToParentViewController:self];
// remove child view
UIViewController *vc = [self.childViewControllers lastObject];
[vc.view removeFromSuperview];
extension String {
func md5() -> String! {
let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
let strLen = CUnsignedInt(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
let digestLen = Int(CC_MD5_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)
CC_MD5(str!, strLen, result)
var hash = NSMutableString()
@convexstyle
convexstyle / gist:bccd27af7f2b6d2ab66c
Last active August 29, 2015 14:20
Remove carriage return (^M) in Vim
(1) vim -b something.txt
(2) Escape
(3) :%s/^M//s
To avoid adding ^M in RubyMine or Web Storm,
Preference > General > Uncheck "Use soft wraps in editor"
@convexstyle
convexstyle / cmake error
Created September 10, 2013 09:28
cmake error from installing mysql
=== cmake erros ===
brew install mysql
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/mysql-5.6.13.lion.bottle.tar.gz
######################################################################## 100.0%
Warning: Bottle installation failed: building from source.
Error: /usr/local/opt/cmake not present or broken
Please reinstall cmake. Sorry :(
=== Install cmake and then, mysql ===
brew install cmake
@convexstyle
convexstyle / Cancel_UIViewAnimation
Created September 6, 2013 03:42
Immediately, cancel previous UIView animation.
- (void)startAnimation:(id)sender {
UIView animateWithDuration:2.0f delay:0.0f options:UIViewAnimationCurveEaseInOut animations:^{
_yourView.alpha = 0.0f;
} completion:^(BOOL finished) {
// Do something
}];
}
- (void)stopAnimation:(id)sender {
[UIView setAnimationBeginsFromCurrentState:YES];
@convexstyle
convexstyle / Rails 4 autoload
Last active December 22, 2015 09:28
Autoload custom modules
# in configs/application.rb
module MyApp
class Application < Rails::Application
config.autoload_paths += Dir["#{config.root}/lib"]
end
end
@convexstyle
convexstyle / Rails4_ Avoid_ActiveRecord::RecordNotFound
Last active December 22, 2015 08:38
Avoid ActiveRecord::RecordNotFound
#author = Author.find(id) throws ActiveRecord::RecordNotFound error.
#for one record
author = Author.find_by_id(id)
if !author.nil?
#do something
else
#do something
end
@convexstyle
convexstyle / Custom Toolbar Title in Objective-C
Last active December 21, 2015 00:09
Add a custom title to a toolbar when navigation bar is not available.
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32.0)];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backHandler:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.text = @"Sample Title";
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.backgroundColor = [UIColor clearColor];
@convexstyle
convexstyle / No CSRF form
Created August 9, 2013 11:12
If a generic form is used without CSRF code in Rails, then, add the following line to the controller.
skip_before_filter :verify_authenticity_token ,:only=>[:action_name]
@convexstyle
convexstyle / Facebook Photo Upload and Privacy Setting
Last active December 20, 2015 20:29
Upload Photo to Facebook Timeline with privacy setting using Facebook SDK.
/*
References :
https://developers.facebook.com/docs/reference/api/photo/
https://developers.facebook.com/docs/reference/api/privacy-parameter/
*/
// Upload Image
UIImage *uploadImage = /* assign your UIImage */
//--- Post Parameters ---//