#ifndef __clang_analyzer__
// Code not to be analyzed
#endif
(Source: stackoverflow.com)
- (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];
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
}
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’)
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]
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);
}
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 (?)
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";
}
}