Skip to content

Instantly share code, notes, and snippets.

View priore's full-sized avatar

Priore priore

View GitHub Profile
@priore
priore / gist:3a0bafd04f2e9f0da3c6
Created November 12, 2014 11:17
Convert NSObject to NSDictionary
// NSArray+NSDictionary.m
#import "NSObject+NSDictionary.h"
@implementation NSArray (NSDictionary)
- (NSDictionary*)toDictionary
{
NSMutableDictionary *dict = [NSMutableDictionary new];
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
@priore
priore / gist:1daa138d38f8cee0ff29
Created November 12, 2014 11:15
Tips: a solid dealloc for all classes
- (void)dealloc
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
if ([self respondsToSelector:@selector(setDelegate:)])
[self performSelector:@selector(setDelegate:) withObject:nil];
[super dealloc]; // not needed in ARC
}
@priore
priore / gist:eb8ebee056375d7f8a0e
Created November 12, 2014 11:14
Fix the crash in iOS6 when you start a not supported video stream
// Fix the crash in iOS6 when you start a not supported video stream
// note: do not use initWithUrl, instead use setContentURL
MPMoviePlayerViewController viewController = [[MPMoviePlayerViewController alloc] init];
viewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
viewController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[viewController.moviePlayer setContentURL:url];
@priore
priore / NSDictionary+Values.m
Last active August 29, 2015 14:01
Fixes crash in case of missing of the partial KeyPath in NSDictionary
@implementation NSDictionary (Values)
- (id)valueForKeyPath:(NSString *)keyPath
{
__block id value = nil;
if (keyPath != nil) {
NSArray *keys = [keyPath componentsSeparatedByString:@"."];
if (keys && [keys count] > 0) {
[keys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
@priore
priore / gist:8496221
Last active January 3, 2016 17:29
Convert Unicode string into readable string
NSString *input = @"\\u5404\\u500b\\u90fd";
CFStringTransform((__bridge CFMutableStringRef)input, NULL, CFSTR("Any-Hex/Java"), YES);
NSLog(@"Readable string: %@", input);
@priore
priore / gist:8496208
Created January 18, 2014 20:49
JSON formatter for TextWrangler
# put script in ~/Library/Application Support/TextWrangler/Text Filters/Format JSON.py
#!/usr/bin/python
import fileinput
import json
if __name__ == "__main__":
text = ''
for line in fileinput.input():
text = text + ' ' + line.strip()
jsonObj = json.loads(text)
print json.dumps(jsonObj, sort_keys=True, indent=2)
@priore
priore / gist:8496203
Created January 18, 2014 20:49
XML formatter for TextWrangler
# put script in ~/Library/Application Support/TextWrangler/Text Filters/Tidy XML.py
#!/bin/sh
XMLLINT_INDENT=$'\t' xmllint --format --encode utf-8 -
@priore
priore / gist:8496194
Created January 18, 2014 20:48
UILabel text size fit macro
// UILabel text size fit macro
//
// use :
//
// UILabel *label = ....
// TEXT_FIT(label, @"your text here");
//
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define TEXT_FIT_IOS6(t,txt) (CGRect){t.frame.origin, t.frame.size.width, [txt sizeWithFont:t.font constrainedToSize:t.frame.size lineBreakMode:t.lineBreakMode].height}
#define TEXT_FIT_IOS7(t,txt) (CGRect){t.frame.origin, t.frame.size.width, [txt boundingRectWithSize:t.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:t.font} context:nil].size.height}
@priore
priore / gist:8134505
Created December 26, 2013 14:35
Convert Unicode string into readable string
NSString *input = @"\\u5404\\u500b\\u90fd";
CFStringTransform((__bridge CFMutableStringRef)input, NULL, CFSTR("Any-Hex/Java"), YES);
NSLog(@"Readable string: %@", input);
@priore
priore / gist:7163691
Last active December 26, 2015 14:09
ReviewEngine simplifies the calling to app internet page for requesting a user review of the app on the app store. To use it simply drop the two files into your code and then add one method call to get it working.
//
// ReviewEngine.h
//
// Created by Danilo Priore on 23/11/11.
// Copyright (c) 2011 Prioregroup.com. All rights reserved.
//
// ReviewEngine simplifies the calling to app internet page for requesting a
// user review of the app on the app store. To use it simply drop the two
// files into your code and then add one method call to get it working.