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];
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
}
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’)
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]
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);
}
Strip HTML Tags
using System.Text.RegularExpressions;
...
public static string StripHTML(string htmlText)
{
return Regex.Replace(htmlText, "<(.|\n)*?>", "");
}
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
— Martin Golding (?)
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";
}
}
age from birthday
Rather than dividing figures by a fixed number days (which ignores special cases like leap years), I find this method quite reliable.
//Given DateTime birthday
TimeSpan span = DateTime.Now - birthday;
DateTime time = DateTime.MinValue + span;
int age = time.Year - 1;
1.