Skip to content

Instantly share code, notes, and snippets.

@yannxou
yannxou / RedownloadPods.sh
Last active September 9, 2015 20:49
Cocoapods: Force redownloading dependencies
rm Pods/Manifest.lock && rm Podfile.lock && pod install
@yannxou
yannxou / optionalAssignment.swift
Created October 19, 2015 14:59
Swift: Optional assignment (with double question mark)
// This is a shortcut for user.friends != nil ? user.friends.count : 0
let count = user.friends?.count ?? 0
@yannxou
yannxou / gist:5becfd37658d43b51ef1
Created October 20, 2015 11:32
xcode: Debug CoreData queries
Add the following line at the 'Arguments Passed On Launch' in your run scheme.
-com.apple.CoreData.SQLDebug 1
@yannxou
yannxou / PrivateSetter.swift
Created November 6, 2015 11:15
Swift: Declaring var with private setter
// Var only setteable from private access level but with internal access (default) on its getter
private(set) var info = ""
// Var only setteable from private access level but with public access on its getter
public private(set) var info = ""
@yannxou
yannxou / FileHelper.m
Created January 27, 2016 13:50
Ovjective-c: Clean temporary directory
/**
Deletes all the existing files and subdirectories in the NSTemporaryDirectory
*/
+ (void)cleanTemporaryDirectory {
NSString *tmpDirectory = NSTemporaryDirectory();
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:tmpDirectory error:&error];
for (NSString *file in cacheFiles) {
error = nil;
@yannxou
yannxou / FileHelper.m
Created January 27, 2016 14:23
Objective-c: Download file from url using AFNetworking
/**
Downloads the file from the specified <code>url</code> into the given <code>targetPath</code>.
*/
+ (void)downloadFileFromUrl:(NSString *)url targetPath:(NSString *)path success:(void (^)(NSString *filePath))success failure:(void (^)(NSError *error))failure {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *filename = [operation.response suggestedFilename];
NSString *filePath = [path stringByAppendingPathComponent:filename];
@yannxou
yannxou / gist:b45988a703134edfab8c
Created January 28, 2016 14:52
xcode: Print the View Controller Hierarchy
po [[[[UIApplication sharedApplication] keyWindow] rootViewController] _printHierarchy]
@yannxou
yannxou / multichoice_demo.sh
Last active May 22, 2025 21:10
Bash script to select from multiple options
#!/bin/bash
# customize with your own.
options=("AAA" "BBB" "CCC" "DDD")
menu() {
clear
echo "Avaliable options:"
for i in ${!options[@]}; do
printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${options[i]}"
@yannxou
yannxou / htmlLabel.m
Created May 24, 2016 13:10
Objective-C: Use HTML in UILabel
NSString * htmlString = @" <p><b>Hey</b> you. My <b>name </b> is <h1> Joe </h1></p> ";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel * myLabel = [UILabel alloc] init];
myLabel.attributedText = attrStr;