Skip to content

Instantly share code, notes, and snippets.

View quietcricket's full-sized avatar

Shang Liang quietcricket

  • Singapore
View GitHub Profile
@quietcricket
quietcricket / gist:1593632
Created January 11, 2012 07:58
Fuzzy string match objective-c (Levenshtein Distance Algorithm)
-(float)compareString:(NSString *)originalString withString:(NSString *)comparisonString
{
// Normalize strings
[originalString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[comparisonString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
originalString = [originalString lowercaseString];
comparisonString = [comparisonString lowercaseString];
// Step 1 (Steps follow description at http://www.merriampark.com/ld.htm)
@quietcricket
quietcricket / gist:1593641
Created January 11, 2012 08:01
.gitignore_global
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
# Packages #
@quietcricket
quietcricket / gist:1594228
Created January 11, 2012 11:16
Adding Core Data for Xcode 4 Projects
//
// Initialize the context. This is THE object you are going to use for all the data input and output.
//
NSManagedObjectContext* context=[[NSManagedObjectContext alloc] init];
//
// Initialize the model file, some black magic and you don't need to specify the model file name.
// You can point to it manually but since they introduced "versioned" data model, it's hard to figure out what name or path you should use.
//
NSManagedObjectModel* model=[NSManagedObjectModel mergedModelFromBundles:nil];
//
@quietcricket
quietcricket / gist:1639122
Created January 19, 2012 09:54
Reset MySQL password
#stop server
service mysqld stop
#start server without checking password
mysqld_safe --skip-grant-tables &
#log into server as root and specify "mysql" is the database going to be used
mysql --user=root mysql
#update the password
update user set Password=PASSWORD('new-password-here') WHERE User='root';
#make the changes to take effect
flush privileges;
@quietcricket
quietcricket / gist:1729667
Created February 3, 2012 11:00
Solve & problem in URL
NSString* link=[NSString stringWithFormat:@"https://twitter.com/intent/tweet?url=http://google.com&text=",@"Some text that may have & damn you &&&"];
link=[link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
link=[link stringByReplacingOccurrencesOfString:@"&" withString:@"%26" options:NSLiteralSearch range:NSMakeRange([link rangeOfString:@"&"].location+1, link.length-1)];
@quietcricket
quietcricket / gist:2368138
Created April 12, 2012 15:21
IOS download image asynchronously and cache it
__block ASIHTTPRequest* req=[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:url]];
req.cachePolicy=ASIOnlyLoadIfNotCachedCachePolicy;
req.downloadCache=[ASIDownloadCache sharedCache];
[req setCompletionBlock:^{
UIImage* img=[UIImage imageWithData:[req responseData]];
}];
[req setFailedBlock:^{
NSLog(@"Download Failed");
@quietcricket
quietcricket / gist:2521037
Created April 28, 2012 18:20
Get IP Address, C/C++
string getIPAddress(){
string ipAddress="Unable to get IP Address";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
@quietcricket
quietcricket / gist:2723027
Created May 18, 2012 03:42
Email Validation
- (BOOL) validateEmail: (NSString *) candidate {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:candidate];
}
@quietcricket
quietcricket / gist:3167729
Created July 24, 2012 02:55
Apache multiple domain hosting config
#This line is very important. It tells the server that this part is based on the name of the server, not IP address or anything else.
NameVirtualHost *:80
# * means any ip address is fine. If you want to put, put your current server ip address. It's quite unlikely the server has multiple ip addresses. For normal people, * is good enough
<VirtualHost *:80>
#ServerName is used internally for Apache, so it does not matter what name you give it. As long as it does not conflict with other servers.
ServerName www.domain.tld
#This is the part specifies which domain it is serving. To be safe, always include the one without www and the one with www.
ServerAlias domain.tld www.domain.tld
#The files to be served for this domain.
@quietcricket
quietcricket / gist:3423320
Created August 22, 2012 07:16
Create IOS Push notification certificate
openssl x509 -inform der -in cert.cer -out cert.pem
openssl pkcs12 -nocerts -in key.p12 -out key.pem
cat cert.pem key.pem > joined.pem