I code therefore I am.

February 16, 2012 at 3:48pm
0 notes
Comments (View)

Tags:
objective-c  

Supressing Xcode static analyzer warnings

#ifndef __clang_analyzer__

// Code not to be analyzed

#endif

(Source: stackoverflow.com)

October 24, 2011 at 6:59pm
1 note
Comments (View)

Tags:
Objective-C  

Perform a Block after Delay

- (void)performBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay
{
    int64_t delta = (int64_t)(1.0e9 * delay);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delta), 
        dispatch_get_main_queue(), block);
}
Then, we use:
[self performBlock: ^{ 
    //Lines of code in block to execute
} afterDelay: 0.5f];

August 19, 2011 at 12:49pm
0 notes
Comments (View)

Tags:
Objective-C  

Setting to nil

NSObject *obj;
if (obj != nil) {
    //code will enter here and we'll get a bad access error to obj!
}
vs.
NSObject *obj = nil;
if (obj != nil) {
    //obj is nil so we're safe if we do something with obj
}

February 25, 2011 at 1:16pm
Notes
Comments (View)

Tags:
CSharp  conversion  

Hex String to Color (ARGB)

public Color HexStringToColor(string hex)
{
	hex = hex.Replace("#", "").ToLowerInvariant();

	
	if (hex.Length == 6)
	{
		hex = "ff" + hex;
	}
	else if (hex.Length != 8)
	{
		throw new Exception(hex + " 
			is not a valid 6 or 8-place 
			hexadecimal color code.");
	}

	int a, r, g, b;
	a = Convert.ToInt32(hex.Substring(0, 2), 16);
	r = Convert.ToInt32(hex.Substring(2, 2), 16);
	g = Convert.ToInt32(hex.Substring(4, 2), 16);
	b = Convert.ToInt32(hex.Substring(6, 2), 16);

	return Color.FromArgb(a, r, g, b);
}
(this assumes input is correct format - either ‘#FFFFFF’ or ‘FFFFFF’)

July 26, 2010 at 5:19pm
0 notes
Comments (View)

Tags:
Lua  

Swapping variable values using multiple assignment in Lua

Lua allows multiple assignment, where a list of values is assigned to a list of variables in one step. Both lists have their elements separated by commas. For instance, in the assignment

a, b = 10, 2*x

the variable a gets the value 10 and b gets 2*x.
In a multiple assignment, Lua first evaluates all values and only then executes the assignments. Therefore, we can use a multiple assignment to swap two values, as in

x, y = y, x -- swap `x' for `y'
a[i], a[j] = a[j], a[i] -- swap `a[i]' for `a[j]'

[Source: lua.org: 4.1 Assignment]

April 7, 2010 at 6:32pm
2 notes
Comments (View)

Tags:
CSharp  Mono  

NSDate to DateTime and Back

  • Reference Date is January 1, 2001 0:00:00 UTC (GMT)
  • It’s important to note the Local Time Zone vs. UTC/GMT
NSDate to DateTime
public static DateTime NSDateToDateTime(NSDate date)
{
    DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime( 
        new DateTime(2001, 1, 1, 0, 0, 0) );
    return reference.AddSeconds(date.SecondsSinceReferenceDate);
}
DateTime to NSDate
public static NSDate DateTimeToNSDate(DateTime date)
{
    DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime(
        new DateTime(2001, 1, 1, 0, 0, 0) );
    return NSDate.FromTimeIntervalSinceReferenceDate(
        (date - reference).TotalSeconds);
}

February 19, 2010 at 5:59pm
0 notes
Comments (View)

Tags:
CSharp,  format  regex  

Strip HTML Tags

using System.Text.RegularExpressions;
    ...
    public static string StripHTML(string htmlText)
    {
        return Regex.Replace(htmlText, "<(.|\n)*?>", "");
    }

February 17, 2010 at 2:13pm
0 notes
Comments (View)

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

— Martin Golding (?)

February 5, 2010 at 3:24pm
0 notes
Comments (View)

Tags:
format  datetime  

Unicode Date Format Patterns →

January 22, 2010 at 1:16pm
0 notes
Comments (View)

Tags:
format  CSharp  integer  

Integer to Ordinal

public string AddOrdinal(int num)
{
    switch(num % 100)
    {
        case 11:
        case 12:
        case 13:
            return num.ToString() + "th";
    }

    switch(num % 10)
    {
        case 1:
            return num.ToString() + "st";
        case 2:
            return num.ToString() + "nd";
        case 3:
            return num.ToString() + "rd";
        default:
            return num.ToString() + "th";
    }
}