Skip to content

Instantly share code, notes, and snippets.

View derekli66's full-sized avatar
🤓
Being.a.nerd()

Chien-Ming (Derek) Lee derekli66

🤓
Being.a.nerd()
View GitHub Profile
@derekli66
derekli66 / NSArray+CatchIndexOutOfRange.m
Last active March 22, 2017 06:48
To catch index out of range in NSArray. Use method swizzle to catch out of range exception and NSLog array contents, array count, and the failed index. NSInteger type is used to catch negative index.
// Inspired by Blended Cocoa
// Reference: http://www.blendedcocoa.com/blog/2012/10/25/objective-c-literals-negative-array-subscripts/
#import <objc/runtime.h>
@implementation NSArray (CatchIndexOutOfRange)
+ (void)load
{
static dispatch_once_t onceToken;
@derekli66
derekli66 / Reinstall_Cocoapods
Last active May 22, 2024 17:59
Reinstall Cocoapods
When pod install or pod update gets hanged at "Analyzing dependencies", please consider to reinstall cocoapods.
#Uninstallation. Reference: http://superuser.com/questions/686317/how-to-fully-uninstall-the-cocoapods-from-the-mac-machine
$gem list --local | grep cocoapods
#The list will show like this
cocoapods (0.27.1, 0.20.2)
cocoapods-core (0.27.1, 0.20.2)
cocoapods-downloader (0.2.0, 0.1.2)
@derekli66
derekli66 / Binary_Converter.c
Last active August 29, 2015 14:19
Change decimal numbers into binary format
char *stradd(const char *a, const char *b){
size_t len = strlen(a) + strlen(b);
char *ret = (char *)malloc(len * sizeof(char) + 1);
return strcat(strcat(ret, a), b);
}
char *binaryConverter(int input_number) {
int number = input_number;
char *binaryString = (char *)malloc(sizeof(char));
// 1. Before using following code snippet, make sure to add textfields on a UIScrollView
// 2. The following code snippet will lead the first responder textfield just top of showing keyboard.
// 3. The trick is to set enough scrolling space for scroll view to raise all textfields up.
// When target textfield is touched, the scroll view will do the animation automatically and raise textfields up.
// 4. Do not combine other technique to raise up textfields over keyboard.
- (instancetype)init
{
self = [super init];
if (self) {
@derekli66
derekli66 / Char_Finder.c
Created April 18, 2015 07:20
The sample code to pointer position of specific char in a string.
size_t strlength(char const *string) {
int length = 0;
while (*string++ != '\0') {
length += 1;
}
return length;
}
char *find_char(char const *source, char const *chars) {
@derekli66
derekli66 / rewind_string.c
Last active September 6, 2015 13:45
(Weird result so far...) Rewind a C string from the last pointer to the first pointer which the start of the string.
char *rewind_string(char *last_char) {
// Prevent NUL byte.
if (*last_char == '\0' || last_char == NULL) {
return NULL;
}
char *first_char;
// Rewind to the pointer pointing to first character.
for (first_char = last_char; *first_char != '\0'; first_char--);
@derekli66
derekli66 / del_str.c
Last active September 6, 2015 13:57
Programming exercise 6-3 in chapter 6 of Pointers on C.
#include <stdio.h>
#include <stdlib.h>
#include <String.h>
/*
* === FUNCTION ======================================================================
* Name: del_str
* Description: Delete substring in the source string
* =====================================================================================
*/
@derekli66
derekli66 / UnNull.h
Created May 2, 2015 14:38
Functions to check nil or null object input for NSNumber and NSString.
/**
Inspect input number to see if there is nil input or null object input
@param aNumber an input number
@return If nil input or null object input found, a NSNumber object with zero value will return.
If the input is a NSStirng object, a NSNumber object contianing dobleValue of the string will return.
@warning If input number is not a NSNumber or NSString, a NSNumber object containing zero will return.
*/
@derekli66
derekli66 / Find_Prime_Number.c
Created May 21, 2015 03:34
Programming exercise 6-4 in book, Pointers on C.
void FindPrimeNumber(int size, int results[]) {
// create elements array
int *elements;
elements = (int *)malloc(size * sizeof(int));
for (int i = 0; i < size ; i++) {
elements[i] = 1 ;
}
//start to cross out. Beginning from i = 2
@derekli66
derekli66 / reverse_string.c
Created August 15, 2015 05:14
C function to reverse a string
void
reverseStr(char *str)
{
int len = 0;
int init = 0;
while (*(str + len) != '\0') {
len++;
}